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

如何在Web请求后在C#中反序列化复杂的嵌套Json数据,第1张

概述好的,所以基本上如果你看下面的代码我已经向google api发了一个Web请求,它将日历信息作为一个字符串返回,但是我在访问嵌套的Json数据时遇到了麻烦.我做了很多搜索,说实话,我完全迷失了.这是我发出Web请求并将Json数据传递给字符串的代码: WebRequest request = WebRequest.Create("http://www.google.com/calendar/fe 好的,所以基本上如果你看下面的代码我已经向Google Api发了一个Web请求,它将日历信息作为一个字符串返回,但是我在访问嵌套的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 Data的字符串,我只能使用以下类访问版本和编码组件:

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

到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
总结

以上是内存溢出为你收集整理的如何在Web请求后在C#中反序列化复杂的嵌套Json数据全部内容,希望文章能够帮你解决如何在Web请求后在C#中反序列化复杂的嵌套Json数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1064622.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存