4

好的,所以基本上,如果您查看以下代码,我已经向 google api 发出了一个 web 请求,该 api 将日历信息作为字符串返回,但是我在访问嵌套的 Json 数据时遇到了麻烦。我已经做了很多搜索,老实说我完全迷路了。这是我发出网络请求并将 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 数据的最佳方式是什么......上面的网络请求中提供的 url 应该可以工作。

另外,我在 Xamarin 工作室工作,所以我使用 Newtonsoft.Json 进行反序列化。

4

1 回答 1

5

将其反序列化为 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
于 2013-04-16T15:28:33.143 回答