0

Currently I am trying to deserialize a json string to a list of objects. I have this json string:

{
"begin_date": "2016-02-01",
"end_date": "2016-02-11",
"query_type": "ap",
"sorties": [
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-03",
"tkof": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"ldg": {
"time": "23:16",
"loc": "EHTL",
"rwy": "2"
},
"dalt": "20",
"dt": ""
},
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "S",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-03",
"tkof": {
"time": "23:17",
"loc": "EHTL",
"rwy": "0"
},
"ldg": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"dalt": "10",
"dt": ""
},
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "S",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-04",
"tkof": {
"time": "14:54",
"loc": "EHTL",
"rwy": "32"
},
"ldg": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"dalt": "250",
"dt": ""
},
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-04",
"tkof": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"ldg": {
"time": "23:12",
"loc": "EHTL",
"rwy": "19"
},
"dalt": "10",
"dt": ""
},
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "S",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-05",
"tkof": {
"time": "13:05",
"loc": "EHTL",
"rwy": "32"
},
"ldg": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"dalt": "0",
"dt": ""
},
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-05",
"tkof": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"ldg": {
"time": "13:19",
"loc": "EHTL",
"rwy": "14"
},
"dalt": "0",
"dt": ""
},
{
"id": "icao:440044",
"cs": "GEZC",
"launch": "S",
"tow_id": "",
"tow_name": "",
"type": 15,
"date": "2016-02-05",
"tkof": {
"time": "19:59",
"loc": "EHTL",
"rwy": "23"
},
"ldg": {
"time": "",
"loc": "EHTL",
"rwy": ""
},
"dalt": "0",
"dt": ""
}
],
"sum_dt": "0",
"first_tkof": "23:17",
"last_ldg": "13:19",
"max_dalt": 250
}

I want to store all the sorties into a list of objects that i've created. So I can easily work with it. I know I could use Newtonsoft.Json.JsonConvert.DeserializeObject but I only know how to do it on non nested json strings.

  public class Flight
    {

        public string id { get; set; }
        public string cs { get; set; }
        public string launch { get; set; }
        public string tow_id { get; set; }
        public string tow_name { get; set; }
        public int type { get; set; }
        public string date { get; set; }
        public string tkof { get; set; }
        public string ldg { get; set; }
        public string dalt { get; set; }
        public string dt { get; set; }
    }
2
  • You can try this: blog.codeinside.eu/2014/09/08/… Commented Feb 11, 2016 at 19:05
  • 1
    As an aside, you should really get into the habit of giving your properties meaningful names. Code is generally write once - read many. Would people reading your code immediately know what tkof, ldg and dalt are? Commented Feb 11, 2016 at 19:23

4 Answers 4

1

You can deserialize it using this:

void Main()
{
    const string testJson = @"{""begin_date"": ""2016-02-01"",""end_date"": ""2016-02-11"",""query_type"": ""ap"",""sorties"": [{""id"": ""icao:440044"",""cs"": ""GEZC"",""launch"": """",""tow_id"": """",""tow_name"": """",""type"": 15,""date"": ""2016-02-03"",""tkof"": {""time"": """",""loc"": ""EHTL"",""rwy"": """"},""ldg"": {""time"": ""23:16"",""loc"": ""EHTL"",""rwy"": ""2""},""dalt"": ""20"",""dt"": """"},{""id"": ""icao:440044"",""cs"": ""GEZC"",""launch"": ""S"",""tow_id"": """",""tow_name"": """",""type"": 15,""date"": ""2016-02-03"",""tkof"": {""time"": ""23:17"",""loc"": ""EHTL"",""rwy"": ""0""},""ldg"": {""time"": """",""loc"": ""EHTL"",""rwy"": """"},""dalt"": ""10"",""dt"": """"},{""id"": ""icao:440044"",""cs"": ""GEZC"",""launch"": ""S"",""tow_id"": """",""tow_name"": """",""type"": 15,""date"": ""2016-02-04"",""tkof"": {""time"": ""14:54"",""loc"": ""EHTL"",""rwy"": ""32""},""ldg"": {""time"": """",""loc"": ""EHTL"",""rwy"": """"},""dalt"": ""250"",""dt"": """"},{""id"": ""icao:440044"",""cs"": ""GEZC"",""launch"": """",""tow_id"": """",""tow_name"": """",""type"": 15,""date"": ""2016-02-04"",""tkof"": {""time"": """",""loc"": ""EHTL"",""rwy"": """"},""ldg"": {""time"": ""23:12"",""loc"": ""EHTL"",""rwy"": ""19""},""dalt"": ""10"",""dt"": """"},{""id"": ""icao:440044"",""cs"": ""GEZC"",""launch"": ""S"",""tow_id"": """",""tow_name"": """",""type"": 15,""date"": ""2016-02-05"",""tkof"": {""time"": ""13:05"",""loc"": ""EHTL"",""rwy"": ""32""},""ldg"": {""time"": """",""loc"": ""EHTL"",""rwy"": """"},""dalt"": ""0"",""dt"": """"},{""id"": ""icao:440044"",""cs"": ""GEZC"",""launch"": """",""tow_id"": """",""tow_name"": """",""type"": 15,""date"": ""2016-02-05"",""tkof"": {""time"": """",""loc"": ""EHTL"",""rwy"": """"},""ldg"": {""time"": ""13:19"",""loc"": ""EHTL"",""rwy"": ""14""},""dalt"": ""0"",""dt"": """"},{""id"": ""icao:440044"",""cs"": ""GEZC"",""launch"": ""S"",""tow_id"": """",""tow_name"": """",""type"": 15,""date"": ""2016-02-05"",""tkof"": {""time"": ""19:59"",""loc"": ""EHTL"",""rwy"": ""23""},""ldg"": {""time"": """",""loc"": ""EHTL"",""rwy"": """"},""dalt"": ""0"",""dt"": """"}],""sum_dt"": ""0"",""first_tkof"": ""23:17"",""last_ldg"": ""13:19"",""max_dalt"": 250}";
    Root root = JsonConvert.DeserializeObject<Root>(testJson);
}

public class Tkof
{

    [JsonProperty("time")]
    public string Time { get; set; }

    [JsonProperty("loc")]
    public string Loc { get; set; }

    [JsonProperty("rwy")]
    public string Rwy { get; set; }
}

public class Ldg
{
    [JsonProperty("time")]
    public string Time { get; set; }

    [JsonProperty("loc")]
    public string Loc { get; set; }

    [JsonProperty("rwy")]
    public string Rwy { get; set; }
}

public class Sorty
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("cs")]
    public string Cs { get; set; }

    [JsonProperty("launch")]
    public string Launch { get; set; }

    [JsonProperty("tow_id")]
    public string TowId { get; set; }

    [JsonProperty("tow_name")]
    public string TowName { get; set; }

    [JsonProperty("type")]
    public int Type { get; set; }

    [JsonProperty("date")]
    public string Date { get; set; }

    [JsonProperty("tkof")]
    public Tkof Tkof { get; set; }

    [JsonProperty("ldg")]
    public Ldg Ldg { get; set; }

    [JsonProperty("dalt")]
    public string Dalt { get; set; }

    [JsonProperty("dt")]
    public string Dt { get; set; }
}

public class Root
{
    [JsonProperty("begin_date")]
    public string BeginDate { get; set; }

    [JsonProperty("end_date")]
    public string EndDate { get; set; }

    [JsonProperty("query_type")]
    public string QueryType { get; set; }

    [JsonProperty("sorties")]
    public Sorty[] Sorties { get; set; }

    [JsonProperty("sum_dt")]
    public string SumDt { get; set; }

    [JsonProperty("first_tkof")]
    public string FirstTkof { get; set; }

    [JsonProperty("last_ldg")]
    public string LastLdg { get; set; }

    [JsonProperty("max_dalt")]
    public int MaxDalt { get; set; }
}

I typically use Xamasoft Json Class Generator, and prefer it over json2csharp due to the app inserting JsonProperty annotations for me, instead of setting all the properties to lower case.

Here's the structure of the deserialized JSON.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

The addition of annotations is quite handy - Thanks for the share
1

http://jsonutils.com/ - The problem is within your class structure. This tool defines classes and their structure, based on the json data you give to it. Very handy. Gives you a baseline to work with then all you have to do is edit the output to your specific needs.

After your structure supports the deserialization of the entire string, just deserialize as you would've before and you'll find that the issue would have resolved itself.

However - for clarity, you need a 'root' class that contains a definition of a List<> - in this example List<Sorty> - Which will allow your deserializer of choice to populate Sorty class objects then add those objects as new List's elements.

The following is purely illustrative, please use a tool (like the one linked above) for the a valid class structure.

//Root Class
public class Flight
{
    public string begin_date { get; set; }
    public string end_date { get; set; }
    public string query_type { get; set; }
    public IList<Sorty> sorties { get; set; } //Bam.. how it works
    public string sum_dt { get; set; }
    public string first_tkof { get; set; }
    public string last_ldg { get; set; }
    public int max_dalt { get; set; }
}

//That list contains populated objects of this Sorty class
public class Sorty
{
    public string id { get; set; }
    public string cs { get; set; }
    public string launch { get; set; }
    public string tow_id { get; set; }
    public string tow_name { get; set; }
    public int type { get; set; }
    public string date { get; set; }
    public Tkof tkof { get; set; }
    public Ldg ldg { get; set; }
    public string dalt { get; set; }
    public string dt { get; set; }
}

Hope that makes sense, check out that website. Truly helpful.

1 Comment

One of my favorite little json tool.
0

Parse the string as Json object -

JObject jsonObj = JObject.Parse(string);

Here is your Main Class -

public class RootObject
{
public string begin_date { get; set; }
public string end_date { get; set; }
public string query_type { get; set; }
public List<Sorty> sorties { get; set; }
public string sum_dt { get; set; }
public string first_tkof { get; set; }
public string last_ldg { get; set; }
public int max_dalt { get; set; }
}

create object of class-

RootObject obj = new RootObject();

Then you can set the values as follows -

obj.begin_date = jsonObj.Value<string>("begin_date");
obj.end_date = jsonObj.Value<string>("end_date");
obj.query_type = jsonObj.Value<string>("query_type");

and So on...

Comments

0

Using newtonsoft

public class Flight
{
        public string begin_date {get;set;}
        public string end_date {get;set;}
        public string query_type {get;set;}
        public List<Sorties> sorties{get;set;}
        public string sum_dt {get;set;}
        public string first_tkof {get;set;}
        public string last_ldg {get;set;}
        public string max_dalt {get;set;}
}

public class Sorties
{
    public string id {get;set;}
    public string cs {get;set;}
    public string launch {get;set;}
    public string tow_id {get;set;}
    public string tow_name {get;set;}
    public int type {get;set;}
    public string date {get;set;}
    public Tkof tkof{get;set;}
    public Ldg ldg{get;set;}
    public string dalt{get;set;}
    public string dt{get;set;}


}
public class Tkof
{
    public string time {get;set;}
    public string loc {get;set;}
    public string rwy {get;set;}
}
public class Ldg
{
    public string time {get;set;}
    public string loc {get;set;}
    public string rwy {get;set;}
}

Usage
var json = "....."
var flightsList = JsonConvert.DeserializeObject<Flight>(json);
var sorties = flightsList.sorties;

foreach(var sorty in sorties)
{
 //use object
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.