Samantha
Samantha

Reputation: 11

How to deserialise specific keys of json object?

Input JSON file:

{
    "@version": "2.7.0",
    "@generated": "Wed, 30 May 2018 17:23:14",
    "site": {
        "@name": "http://google.com",
        "@host": "google.com",
        "@port": "80",
        "@ssl": "false",
        "alerts": [
            {

                "alert": "X-Content-Type-Options Header Missing",
                "name": "X-Content-Type-Options Header Missing",
                "riskcode": "1",
                "confidence": "2",
                "riskdesc": "Low (Medium)",
                "desc": "<p>The Anti-MIME-Sniffing header X-Content-Type-Options was not set to 'nosniff'. This allows older versions of Internet Explorer and Chrome to perform MIME-sniffing on the response body, potentially causing the response body to be interpreted and displayed as a content type other than the declared content type. Current (early 2014) and legacy versions of Firefox will use the declared content type (if one is set), rather than performing MIME-sniffing.</p>",
                "instances": [
                    {
                        "uri": "http://google.com",
                        "method": "GET",
                        "param": "X-Content-Type-Options"
                    }
                ],          
                "wascid": "15",
                "sourceid": "3"
            }

        ]
    }
}

Expected Output: List alerts;

where:

public class Alert
{
    public string alert;
    public string riskcode;
}

I want to fetch particular keys of the json object ahe deserialise it in the alert object.

Upvotes: 1

Views: 79

Answers (3)

Omar Muscatello
Omar Muscatello

Reputation: 1301

Short version

var siteAlerts = JsonConvert.DeserializeObject<dynamic>(json).site.alerts.ToObject<Alert[]>();

Upvotes: 1

CodeNotFound
CodeNotFound

Reputation: 23220

I recommend you to use Newtonsoft.Json library to make it easy for deserializing json data.

If you want a partial deserialization e.g. only deserializing the alerts property into your class Alert without creating the whole strcuture of classes required.

You can use this code:

JObject jObject = JObject.Parse(json);
var alerts = jObject["site"]["alerts"].ToObject<Alert[]>();
foreach(var item in alerts)
{
    Console.WriteLine("alert: " + item.alert);  
    Console.WriteLine("riskcode: " + item.riskcode);
}

Complete demo available here.

Upvotes: 1

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391456

The simplest way is to just declare the outer objects with enough keys to reach the keys you care about:

public class Alert
{
    public string alert;
    public string riskcode;
}

public class SiteAlerts
{
    public Site site { get; set; }
}

public class Site
{
    public List<Alert> alerts { get; } = new List<Alert>();
}

Then you can simply deserialize with:

var siteAlerts = JsonConvert.DeserializeObject<SiteAlerts>(json);
var alerts = siteAlerts.site.alerts; // no error-checking here

Upvotes: 4

Related Questions