0

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; }
}
Federico Dipuma
  • 17,655
  • 4
  • 39
  • 56
Maccord
  • 3
  • 3
  • yeah, you can declare required properties in the class and use it for deserializtion. Share your code and JSON string and the fields that you need – Jayakrishnan May 22 '17 at 16:25
  • I think I had the same problem as you. This may be helpful. https://stackoverflow.com/questions/39962368/deserializing-anything-using-json-net – joelc May 22 '17 at 17:20

2 Answers2

1

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.

Federico Dipuma
  • 17,655
  • 4
  • 39
  • 56
  • Ok, but what if i my response has the same class two times? Something like this: "data" : [ { "dimensions" : [ { "name" : "мужской", "id" : "male" } ], "metrics" : [ [ 19.0, 454.0, 20.0, 5.0, 10.0, 3.0 ], [ 13, 136.85, 156.6, 142.6, 94.66666667 ] ] }, { "dimensions" : [ { "name" : "женский", "id" : "female" } ], "metrics" : [ [0.0 ], [ 0, 0.0 ] ] – Maccord May 22 '17 at 16:31
  • That makes no difference. But I cannot help you if you do not show your json and/or your classes. Edit your post if you have other doubts. – Federico Dipuma May 22 '17 at 16:33
  • What issue do you have? From what I can see there is nothing strange in your classes, just remove the fields you do not want to deserialize, following my answer, and you are done. – Federico Dipuma May 22 '17 at 16:46
  • Json: "data" : [ { "dimensions" : [ { "name" : "мужской", "id" : "male" } ], "dimensions" : [ { "name" : "женский", "id" : "female" } ], Class: public class Dimension { public string name { get; set; } public string id { get; set; } } How do i print both ID's if they both go into this class? – Maccord May 22 '17 at 17:03
  • The problem is that i have two "ID" in Json and I am not sure how to print both. with the following class. – Maccord May 22 '17 at 17:20
  • You have two fields `id` inside **different** json objects. I'm sorry but I truly do not understand your problem. Just deserialize your Json response and use whatever field you want. Here it is a [.NET Fiddle of example](https://dotnetfiddle.net/mhr3Ox). – Federico Dipuma May 22 '17 at 18:03
  • Thank you, I finally got it. I am sorry, I did not know that they were different, it's my first time working with JSON.net. – Maccord May 22 '17 at 18:41
  • Can you please show me how to print all numbers in public List> metrics { get; set; } ? I dont know how to make a foreach algorithm for it. – Maccord May 22 '17 at 20:58
0

I think I had the same problem as you. This may be helpful.

Deserializing anything using JSON.NET

The net-net of it is that you can deserialize to a JToken and then process based on the type of the JToken.

An alternative would be to deserialize into a JObject, or, deserialize into a Dictionary. For embedded dictionaries and lists, the value of the KeyValuePair would be a JObject which you could then parse/process.

joelc
  • 2,687
  • 5
  • 40
  • 60