简体   繁体   English

将复杂的Json反序列化时出错

[英]Error with deserializing complex Json

I deserialize complex JSON (Spotify Playlist) and get root level values but I cannot get branches values. 我反序列化复杂的JSON(Spotify播放列表)并获取根级别值,但无法获取分支值。 I Google problem, try different options without success but, I assume its silly mistake or lack of knowledge so therefore just ask for help what I am missing? 我是Google问题,尝试不同的选择没有成功,但是,我认为它是愚蠢的错误或缺乏知识,因此只寻求帮助是我所缺少的吗?

My classes are: 我的课程是:

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; }
}

And code to deserialize looks like that: 反序列化的代码如下所示:

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()
}

I genereated the classes using this excellent site: json2csharp.com 我使用这个出色的网站生成了这些类: json2csharp.com

And used your existing deserialization code successfully with the following classes. 并将您现有的反序列化代码成功用于以下类。 It populated all the data including the child collections (brace yourself, it's a long one): 它填充了包括子集合在内的所有数据(做好准备,这很长):

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; }
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM