好的,所以本质上,如果你看看下面的代码,我已经做了一个web请求的谷歌api,返回日历信息作为一个字符串,但我有麻烦访问嵌套Json数据。我做了很多搜索,老实说,我完全迷路了。以下是我发出web请求并将Json数据传递给字符串的代码:
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();
但是一旦我有包含Json数据的字符串,我似乎只能用以下类访问版本和编码组件:
public class calendarData
{
public string version { get; set; }
public string encoding { get; set; }
public string feed { get; set; }
}
所以,我想我的问题是在这个请求的情况下访问嵌套json数据的最好方法是什么…上述web请求中提供的url应该可以工作。
另外,我在Xamarin工作室工作,所以我使用Newtonsoft。Json来做反序列化
将其反序列化为RootObject类型的对象,然后访问属性/字段。
你可以使用http://json2csharp.com/)或http://jsonpack.com/ModelGenerators/CSharp
生成json类
主要的问题是你似乎有无效的csharp变量名。
:
// 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