c# - 如何在 Web 请求后在 C# 中反序列化复杂的嵌套 Json 数据

标签 c# json.net

好吧,如果您查看以下代码,基本上我已经向 google 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 studio 工作,所以我使用 Newtonsoft.Json 进行反序列化。

最佳答案

将其反序列化为RootObject类型的对象,然后访问属性/字段。

你可以使用 http://json2csharp.com/ ) 或者 http://jsonpack.com/ModelGenerators/CSharp

generate the json classes .

主要问题是您似乎有无效的 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

关于c# - 如何在 Web 请求后在 C# 中反序列化复杂的嵌套 Json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16040910/

相关文章:

c# - 如何导出 C# dll 方法/函数以在 C++ 中使用它

c# - SignalR 帮助将 List<string> 传递给客户端

c# - 将 json 反序列化为对象 : wrapper class workaround

c# - Newtonsoft.Json DeserializeXmlNode 将标签名称从数字更改为 "something"

jquery - Json数据包含NaN会导致ajax请求时出错

c# - 有选择地使用默认的 JSON 转换器

c# - 使用 LINQ to Entities 的外连接查询

c# - SqlDataSource UpdateCommand 参数未更改

c# - 建立团队和开发流程

c# - 重命名继承变量