Reputation: 51
OK, So Essentially if you look at the following code I've made a web request to a google api that returns calendar info as a string, but I'm having trouble accessing the nested Json data. I've done a lot of searching and to be honest I'm completely lost. here's the code where I make the web request and pass the Json data to a string:
WebRequest request = WebRequest.Create("http://www.google.com/calendar/feeds/[email protected]/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 once I have the string containing the Json Data I can only seem to access the version and encoding components with my following class:
public class calendarData
{
public string version { get; set; }
public string encoding { get; set; }
public string feed { get; set; }
}
So, I guess my question is what's the best way to access nested json data in the case of this request...the url provided in the web request above should work.
Also, I'm working in Xamarin studio so I'm using Newtonsoft.Json to do my Deserialization.
Upvotes: 5
Views: 7059
Reputation: 82186
Deserialize it to a object of type RootObject, and then access the properties/fields.
You can use http://json2csharp.com/) or http://jsonpack.com/ModelGenerators/CSharp
The main problem is that you seem to 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's 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
Upvotes: 5