首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反序列化复杂Json时出错

反序列化复杂Json时出错
EN

Stack Overflow用户
提问于 2015-09-30 19:47:04
回答 1查看 97关注 0票数 1

我反序列化复杂的JSON (Spotify Playlist)并获得根级别的值,但无法获得分支的值。我谷歌问题,尝试不同的选择都没有成功,但是,我假设它愚蠢的错误或缺乏知识,因此只是寻求帮助,我错过了什么?

我的类是:

代码语言:javascript
复制
public class Playlist
{
    public string collaborative { get; set; }
    public string description { get; set; }
    //public <ExternalUrls> external_urls { get; set; }     //object
    //public List<Followers> followers { get; set; }        //object
    public string href { get; set; }
    public string id { get; set; }
    //public List<Image> images { get; set; }               //array
    public string name { get; set; }
}

public class Tracks
{
    public string href { get; set; }
    public Item items { get; set; }                        //object
    public string limit { get; set; }
    public string next { get; set; }
    public string offset { get; set; }
    public string previous { get; set; }
    public string total { get; set; }
}

反序列化的代码看起来像这样:

代码语言:javascript
复制
StreamReader responseFromServer = new StreamReader(myWebResponse.GetResponseStream());

var dataResponse = responseFromServer.ReadToEnd();
responseFromServer.Close();

var elements = new JavaScriptSerializer().Deserialize<Playlist>(dataResponse);

RootBuffer.AddRow();
RootBuffer.collaborative = elements.collaborative.ToString();

foreach (Tracks trs in elements.tracks)
{
    TracksBuffer.AddRow();
    TracksBuffer.href = trs.href.ToString()
}
EN

回答 1

Stack Overflow用户

发布于 2015-09-30 21:53:03

我使用这个优秀的站点生成了这些类:json2csharp.com

并通过以下类成功地使用了现有的反序列化代码。它填充了包括子集合在内的所有数据(支撑自己,这是一个很长的集合):

代码语言:javascript
复制
public class Playlist
{
    public bool collaborative { get; set; }
    public string description { get; set; }
    public ExternalUrls external_urls { get; set; }
    public Followers followers { get; set; }
    public string href { get; set; }
    public string id { get; set; }
    public List<Image> images { get; set; }
    public string name { get; set; }
    public Owner owner { get; set; }
    public bool @public { get; set; }
    public string snapshot_id { get; set; }
    public Tracks tracks { get; set; }
    public string type { get; set; }
    public string uri { get; set; }
}

public class ExternalUrls
{
    public string spotify { get; set; }
}

public class Followers
{
    public object href { get; set; }
    public int total { get; set; }
}

public class Image
{
    public object height { get; set; }
    public string url { get; set; }
    public object width { get; set; }
}

public class ExternalUrls2
{
    public string spotify { get; set; }
}

public class Owner
{
    public ExternalUrls2 external_urls { get; set; }
    public string href { get; set; }
    public string id { get; set; }
    public string type { get; set; }
    public string uri { get; set; }
}

public class ExternalUrls3
{
    public string spotify { get; set; }
}

public class AddedBy
{
    public ExternalUrls3 external_urls { get; set; }
    public string href { get; set; }
    public string id { get; set; }
    public string type { get; set; }
    public string uri { get; set; }
}

public class ExternalUrls4
{
    public string spotify { get; set; }
}

public class Image2
{
    public int height { get; set; }
    public string url { get; set; }
    public int width { get; set; }
}

public class Album
{
    public string album_type { get; set; }
    public List<object> available_markets { get; set; }
    public ExternalUrls4 external_urls { get; set; }
    public string href { get; set; }
    public string id { get; set; }
    public List<Image2> images { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public string uri { get; set; }
}

public class ExternalUrls5
{
    public string spotify { get; set; }
}

public class Artist
{
    public ExternalUrls5 external_urls { get; set; }
    public string href { get; set; }
    public string id { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public string uri { get; set; }
}

public class ExternalIds
{
    public string isrc { get; set; }
}

public class ExternalUrls6
{
    public string spotify { get; set; }
}

public class Track
{
    public Album album { get; set; }
    public List<Artist> artists { get; set; }
    public List<object> available_markets { get; set; }
    public int disc_number { get; set; }
    public int duration_ms { get; set; }
    public bool @explicit { get; set; }
    public ExternalIds external_ids { get; set; }
    public ExternalUrls6 external_urls { get; set; }
    public string href { get; set; }
    public string id { get; set; }
    public string name { get; set; }
    public int popularity { get; set; }
    public string preview_url { get; set; }
    public int track_number { get; set; }
    public string type { get; set; }
    public string uri { get; set; }
}

public class Item
{
    public string added_at { get; set; }
    public AddedBy added_by { get; set; }
    public bool is_local { get; set; }
    public Track track { get; set; }
}

public class Tracks
{
    public string href { get; set; }
    public List<Item> items { get; set; }
    public int limit { get; set; }
    public object next { get; set; }
    public int offset { get; set; }
    public object previous { get; set; }
    public int total { get; set; }
}

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32865280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档