反序列化复杂Json

时间:2015-09-30 11:47:04

标签: c# json

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

我的课程是:

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

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

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

1 个答案:

答案 0 :(得分:0)

我使用这个优秀的网站对这些课程进行了处理:json2csharp.com

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

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

希望这有帮助。