How to deserialize complex Json nested data in C # after a web request

OK. So, if you look at the following code, I made a web request in google api that returns the calendar information as a string, but I am having problems accessing the nested Json data. I searched a lot and, frankly, I was completely lost. here is the code where I make a web request and pass the Json data to a string:

WebRequest request = WebRequest.Create("http://www.google.com/calendar/feeds/ developer-calendar@google.com /public/full?alt=json"); request.Credentials = CredentialCache.DefaultCredentials; request.ContentType = "application/json"; WebResponse response = request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader (dataStream); string responseFromServer = reader.ReadToEnd(); 

but as soon as I have a line containing Json Data, I can only access the version and encoding components with the following class:

  public class calendarData { public string version { get; set; } public string encoding { get; set; } public string feed { get; set; } } 

So, I think my question is the best way to access the json nested data in case of this request ... The URL provided in the web request above should work.

In addition, I work in the Xamarin studio, so I use Newtonsoft.Json to do my deserialization.

+4
source share
1 answer

Detach it to an object of type RootObject, and then access the properties / fields.

You can use http://json2csharp.com/ ) or http://jsonpack.com/ModelGenerators/CSharp

to create json classes .

The main problem is that you have invalid csharp variable names.

Like this:

 // Json Mapping Automatically Generated By JsonToolkit Library for C# // Diego Trinciarelli 2011 // To use this code you will need to reference Newtonsoft Json Parser, downloadable from codeplex. // http://json.codeplex.com/ // using System; using Newtonsoft.Json; namespace MyNameSpace{ [Serializable] class MyMainClass { public string Version; public string Encoding; public Feed Feed; //Empty Constructor public MyMainClass(){} public string Serialize() { return JsonConvert.SerializeObject(this); } public static MyMainClass FromJson(string json) { return JsonConvert.DeserializeObject<MyMainClass>(json); } } [Serializable] class Id { public string $t; //Empty Constructor public Id(){} } [Serializable] class Updated { public string $t; //Empty Constructor public Updated(){} } [Serializable] class Category { public string Scheme; public string Term; //Empty Constructor public Category(){} } [Serializable] class Title { public string $t; public string Type; //Empty Constructor public Title(){} } [Serializable] class Subtitle { public string $t; public string Type; //Empty Constructor public Subtitle(){} } [Serializable] class Link { public string Rel; public string Type; public string Href; //Empty Constructor public Link(){} } [Serializable] class Name { public string $t; //Empty Constructor public Name(){} } [Serializable] class Email { public string $t; //Empty Constructor public Email(){} } [Serializable] class Author { public Name Name; public Email Email; //Empty Constructor public Author(){} } [Serializable] class Generator { public string $t; public string Version; public string Uri; //Empty Constructor public Generator(){} } [Serializable] class OpenSearch$totalResults { public int $t; //Empty Constructor public OpenSearch$totalResults(){} } [Serializable] class OpenSearch$startIndex { public int $t; //Empty Constructor public OpenSearch$startIndex(){} } [Serializable] class OpenSearch$itemsPerPage { public int $t; //Empty Constructor public OpenSearch$itemsPerPage(){} } [Serializable] class GCal$timezone { public string Value; //Empty Constructor public GCal$timezone(){} } [Serializable] class GCal$timesCleaned { public int Value; //Empty Constructor public GCal$timesCleaned(){} } [Serializable] class Id2 { public string $t; //Empty Constructor public Id2(){} } [Serializable] class Published { public string $t; //Empty Constructor public Published(){} } [Serializable] class Updated2 { public string $t; //Empty Constructor public Updated2(){} } [Serializable] class Category2 { public string Scheme; public string Term; //Empty Constructor public Category2(){} } [Serializable] class Title2 { public string $t; public string Type; //Empty Constructor public Title2(){} } [Serializable] class Content { public string $t; public string Type; //Empty Constructor public Content(){} } [Serializable] class Link2 { public string Rel; public string Type; public string Href; public string Title; //Empty Constructor public Link2(){} } [Serializable] class Name2 { public string $t; //Empty Constructor public Name2(){} } [Serializable] class Email2 { public string $t; //Empty Constructor public Email2(){} } [Serializable] class Author2 { public Name2 Name; public Email2 Email; //Empty Constructor public Author2(){} } [Serializable] class Gd$feedLink { public string Href; //Empty Constructor public Gd$feedLink(){} } [Serializable] class Gd$comments { public Gd$feedLink Gd$feedLink; //Empty Constructor public Gd$comments(){} } [Serializable] class Gd$eventStatus { public string Value; //Empty Constructor public Gd$eventStatus(){} } [Serializable] class Gd$where { public string ValueString; //Empty Constructor public Gd$where(){} } [Serializable] class Gd$attendeeStatus { public string Value; //Empty Constructor public Gd$attendeeStatus(){} } [Serializable] class Gd$who { public string Email; public string Rel; public string ValueString; public Gd$attendeeStatus Gd$attendeeStatus; //Empty Constructor public Gd$who(){} } [Serializable] class Gd$when { public string EndTime; public string StartTime; //Empty Constructor public Gd$when(){} } [Serializable] class Gd$transparency { public string Value; //Empty Constructor public Gd$transparency(){} } [Serializable] class GCal$anyoneCanAddSelf { public string Value; //Empty Constructor public GCal$anyoneCanAddSelf(){} } [Serializable] class GCal$guestsCanInviteOthers { public string Value; //Empty Constructor public GCal$guestsCanInviteOthers(){} } [Serializable] class GCal$guestsCanModify { public string Value; //Empty Constructor public GCal$guestsCanModify(){} } [Serializable] class GCal$guestsCanSeeGuests { public string Value; //Empty Constructor public GCal$guestsCanSeeGuests(){} } [Serializable] class GCal$sequence { public int Value; //Empty Constructor public GCal$sequence(){} } [Serializable] class GCal$uid { public string Value; //Empty Constructor public GCal$uid(){} } [Serializable] class Gd$when2 { public string StartTime; //Empty Constructor public Gd$when2(){} } [Serializable] class Gd$originalEvent { public string Href; public string Id; public Gd$when2 Gd$when; //Empty Constructor public Gd$originalEvent(){} } [Serializable] class Gd$recurrence { public string $t; //Empty Constructor public Gd$recurrence(){} } [Serializable] class Entry { public Id2 Id; public Published Published; public Updated2 Updated; public Category2[] Category; public Title2 Title; public Content Content; public Link2[] Link; public Author2[] Author; public Gd$comments Gd$comments; public Gd$eventStatus Gd$eventStatus; public Gd$where[] Gd$where; public Gd$who[] Gd$who; public Gd$when[] Gd$when; public Gd$transparency Gd$transparency; public GCal$anyoneCanAddSelf GCal$anyoneCanAddSelf; public GCal$guestsCanInviteOthers GCal$guestsCanInviteOthers; public GCal$guestsCanModify GCal$guestsCanModify; public GCal$guestsCanSeeGuests GCal$guestsCanSeeGuests; public GCal$sequence GCal$sequence; public GCal$uid GCal$uid; public Gd$originalEvent Gd$originalEvent; public Gd$recurrence Gd$recurrence; //Empty Constructor public Entry(){} } [Serializable] class Feed { public string Xmlns; public string Xmlns$openSearch; public string Xmlns$gCal; public string Xmlns$gd; public Id Id; public Updated Updated; public Category[] Category; public Title Title; public Subtitle Subtitle; public Link[] Link; public Author[] Author; public Generator Generator; public OpenSearch$totalResults OpenSearch$totalResults; public OpenSearch$startIndex OpenSearch$startIndex; public OpenSearch$itemsPerPage OpenSearch$itemsPerPage; public GCal$timezone GCal$timezone; public GCal$timesCleaned GCal$timesCleaned; public Entry[] Entry; //Empty Constructor public Feed(){} } } //Json Mapping End 
+5
source

All Articles