Maccord Maccord - 1 year ago 83
C# Question

Json deserialization without declaration of a class

I have a http response in json format which I need to deserialize using JSON.Net library. I used http://json2csharp.com/ to create classes which I will need, but is there a way to do this without declaring all of them? I only need 3-4 out of 20 declared fields.

The Response:

{
"query": {
"ids": [42354854],
"dimensions": ["ym:s:gender"],
"metrics": ["ym:s:visits", "ym:s:users", "ym:s:avgVisitDurationSeconds"],
"sort": ["-ym:s:visits"],
"date1": "2017-03-01",
"date2": "2017-05-09",
"group": "Week",
"auto_group_size": "1",
"quantile": "50",
"attribution": "Last",
"currency": "RUB",
"auto_group_type": "week"
},
"data": [{
"dimensions": [{
"name": "мужской",
"id": "male"
}],
"metrics": [
[19.0, 42.0, 58.0, 24.0, 13.0, 42.0, 54.0, 20.0, 5.0, 10.0, 3.0],
[11.0, 17.0, 15.0, 12.0, 5.0, 13.0, 15.0, 4.0, 4.0, 5.0, 2.0],
[227.26315789, 275.85714286, 217.29310345, 312.54166667, 42.07692308, 119.38095238, 120.12962963, 136.85, 156.6, 142.6, 94.66666667]
]
}, {
"dimensions": [{
"name": "женский",
"id": "female"
}],
"metrics": [
[6.0, 18.0, 19.0, 3.0, 0.0, 2.0, 4.0, 0.0, 1.0, 0.0, 0.0],
[2.0, 4.0, 5.0, 3.0, 0.0, 1.0, 2.0, 0.0, 1.0, 0.0, 0.0],
[1073.0, 163.66666667, 158.42105263, 20.0, 0.0, 23.5, 12.75, 0.0, 21.0, 0.0, 0.0]
]
}],
"total_rows": 11,
"total_rows_rounded": false,
"sampled": false,
"sample_share": 1.0,
"sample_size": 414,
"sample_space": 414,
"data_lag": 81,
"totals": [
[25.0, 60.0, 77.0, 27.0, 13.0, 44.0, 58.0, 20.0, 6.0, 10.0, 3.0],
[13.0, 21.0, 20.0, 15.0, 5.0, 14.0, 17.0, 4.0, 5.0, 5.0, 2.0],
[430.24, 242.2, 202.76623377, 280.03703704, 42.07692308, 115.02272727, 112.72413793, 136.85, 134.0, 142.6, 94.66666667]
],
"time_intervals": [
["2017-03-01", "2017-03-05"],
["2017-03-06", "2017-03-12"],
["2017-03-13", "2017-03-19"],
["2017-03-20", "2017-03-26"],
["2017-03-27", "2017-04-02"],
["2017-04-03", "2017-04-09"],
["2017-04-10", "2017-04-16"],
["2017-04-17", "2017-04-23"],
["2017-04-24", "2017-04-30"],
["2017-05-01", "2017-05-07"],
["2017-05-08", "2017-05-09"]
]
}


And the classes from the site:

public class Query
{
public List<int> ids { get; set; }
public List<string> dimensions { get; set; }
public List<string> metrics { get; set; }
public List<string> sort { get; set; }
public string date1 { get; set; }
public string date2 { get; set; }
public string group { get; set; }
public string auto_group_size { get; set; }
public string quantile { get; set; }
public string attribution { get; set; }
public string currency { get; set; }
public string auto_group_type { get; set; }
}

public class Dimension
{
public string name { get; set; }
public string id { get; set; }
}

public class Datum
{
public List<Dimension> dimensions { get; set; }
public List<List<double>> metrics { get; set; }
}

public class RootObject
{
public Query query { get; set; }
public List<Datum> data { get; set; }
public int total_rows { get; set; }
public bool total_rows_rounded { get; set; }
public bool sampled { get; set; }
public double sample_share { get; set; }
public int sample_size { get; set; }
public int sample_space { get; set; }
public int data_lag { get; set; }
public List<List<double>> totals { get; set; }
public List<List<string>> time_intervals { get; set; }
}

Answer Source

Create a class that contains only the field you want to be deserialized, Json.NET is smart enough to deserialize your Json object into those fields:

Example Json:

{
    "foo": "bar",
    "foo1": {
        "foo2": 2,
        "foo3": "bar1",
    },
    "foo4": 12
}

May be deserialized into:

public class FooContainer
{
    public string Foo { get; set; }
    public int Foo4 { get; set; }
}

This way foo1 property will be ignored.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download