Adnan Yaseen
Adnan Yaseen

Reputation: 853

Deserializing JSON with Child and Inner Childs

I am familiar with JSON.net a bit and can Deserialize the JSON with basic structure (upto one child). I am currently in process of Deserializing the JSON that is returned from Netatmo API. The structure of JSON is complicated for me. Following is the basic structure of the JSON,

_id
place
   location
        Dynamic Value 1
        Dynamic Value2
   altitude
   timezone
mark
measures
    Dynamic Value 1
    res
        Dynamic Value 1
              Dynamic Value 1
          Dynamic Value 2
    type
        Dynamic Value 1
        Dynamic Value 2
modules
    Dynamic Value 1

Dynamic Value 1 and Dynamic Value 2 represents the values that is changed for each id. The complete JSON is given below,

{
    "body": [{
        "_id": "70:ee:50:02:b4:8c",
        "place": {
            "location": [-35.174779762001, -5.8918476117544],
            "altitude": 52,
            "timezone": "America\/Fortaleza"
        },
        "mark": 0,
        "measures": {
            "02:00:00:02:ba:2c": {
                "res": {
                    "1464014579": [16.7, 77]
                },
                "type": ["temperature", "humidity"]
            },
            "70:ee:50:02:b4:8c": {
                "res": {
                    "1464014622": [1018.1]
                },
                "type": ["pressure"]
            }
        },
        "modules": ["02:00:00:02:ba:2c"]
    }, {
        "_id": "70:ee:50:12:40:cc",
        "place": {
            "location": [-16.074257294385, 11.135715243973],
            "altitude": 14,
            "timezone": "Africa\/Bissau"
        },
        "mark": 14,
        "measures": {
            "02:00:00:06:7b:c8": {
                "res": {
                    "1464015073": [26.6, 78]
                },
                "type": ["temperature", "humidity"]
            },
             "70:ee:50:12:40:cc": {
                "res": {
                    "1464015117": [997]
                },
                "type": ["pressure"]
            }
        },
        "modules": ["02:00:00:06:7b:c8"]
    }],
    "status": "ok",
    "time_exec": 0.010364055633545,
    "time_server": 1464015560
}

I am confused by looking at the complex structure of this JSON. For single level of JSON I have used this code in the past,

IList<lstJsonAttributes> lstSearchResults = new List<lstJsonAttributes>();
foreach (JToken objResult in objResults) {
    lstJsonAttributes objSearchResult = JsonConvert.DeserializeObject<lstJsonAttributes>(objResult.ToString());
    lstSearchResults.Add(objSearchResult);
}

But for so many child I have yet to understand how the object class will be created. Any guidance will highly appreciated.

Update: This is what I have achieved so far.

I have created a main class as below,

public class PublicDataClass
{
    public string _id { get; set; }
    public PublicData_Place place { get; set; }
    public string mark { get; set; }
    public List<string> modules { get; set; }
}

and "Place" class is as follow,

public class PublicData_Place
{
    public List<string> location { get; set; }
    public string altitude { get; set; }
    public string timezone { get; set; }
}

Then I have Deserialized the object in the following code line,

var obj = JsonConvert.DeserializeObject<List<PublicDataClass>>(jsonString);

I can now successfully get all the data except the "measures" which is little bit more complicated.

Upvotes: 2

Views: 15918

Answers (2)

dbc
dbc

Reputation: 116670

Using , JSON objects that have arbitrary property names but fixed schemas for their values can be deserialized as a Dictionary<string, T> for an appropriate type T. See Deserialize a Dictionary for details. Thus your "measures" and "res" objects can be modeled as dictionaries.

You also need a root object to encapsulate your List<PublicDataClass>, since your root JSON container is an object like so: { "body": [{ ... }] }.

Thus you can define your classes as follows:

public class RootObject
{
    public List<PublicDataClass> body { get; set; }
    public string status { get; set; }
    public double time_exec { get; set; }
    public int time_server { get; set; }
}

public class PublicDataClass
{
    public string _id { get; set; }
    public PublicData_Place place { get; set; }
    public int mark { get; set; }
    public List<string> modules { get; set; }
    public Dictionary<string, Measure> measures { get; set; }
}

public class PublicData_Place
{
    public List<double> location { get; set; } // Changed from string to double
    public double altitude { get; set; } // Changed from string to double
    public string timezone { get; set; }
}   

public class Measure
{
    public Measure()
    {
        this.Results = new Dictionary<string, List<double>>();
        this.Types = new List<string>();
    }

    [JsonProperty("res")]
    public Dictionary<string, List<double>> Results { get; set; }

    [JsonProperty("type")]
    public List<string> Types { get; set; }
}

Then do

var root = JsonConvert.DeserializeObject<RootObject>(jsonString);
var obj = root.body;

Upvotes: 5

F&#225;bio Luiz
F&#225;bio Luiz

Reputation: 438

I've worked with XML for a few years and my change to JSON structure I've got a little confused too, always that I want to see how an object look like I use this web site jsoneditoronline Just copy and paste your JSON and click on arrow to parse to an object, I hope it helps until you get used to JSON structure.

Upvotes: 1

Related Questions