反序列化嵌套的JSON字符串,无法访问属性

时间:2018-04-26 20:16:41

标签: c# json

我遇到了从Genius lyric网站API反序列化嵌套JSON数组的问题。我使用http://json2csharp.com制定了对象。当我反序列化对象时,我无法访问类中的属性,这并非完全出乎意料,我只是不确定如何正确设计问题的实际解决方案。 JSON对象转换在未嵌套时工作正常。

处理此问题的最佳方式是什么?

以下是转换代码:

string test = await G.SearchGeniusASync(textBox1.Text);

var data = JsonConvert.DeserializeObject<GeniusApiObject>(test);

这是我的班级:

class GeniusApiObject
{
    public class Meta
    {
        public int status { get; set; }
    }

    public class Stats
    {
        public bool hot { get; set; }
        public int unreviewed_annotations { get; set; }
        public int concurrents { get; set; }
        public int pageviews { get; set; }
    }

    public class PrimaryArtist
    {
        public string api_path { get; set; }
        public string header_image_url { get; set; }
        public int id { get; set; }
        public string image_url { get; set; }
        public bool is_meme_verified { get; set; }
        public bool is_verified { get; set; }
        public string name { get; set; }
        public string url { get; set; }
        public int iq { get; set; }
    }

    public class Result
    {
        public int annotation_count { get; set; }
        public string api_path { get; set; }
        public string full_title { get; set; }
        public string header_image_thumbnail_url { get; set; }
        public string header_image_url { get; set; }
        public int id { get; set; }
        public int lyrics_owner_id { get; set; }
        public string lyrics_state { get; set; }
        public string path { get; set; }
        public int? pyongs_count { get; set; }
        public string song_art_image_thumbnail_url { get; set; }
        public Stats stats { get; set; }
        public string title { get; set; }
        public string title_with_featured { get; set; }
        public string url { get; set; }
        public PrimaryArtist primary_artist { get; set; }
    }

    public class Hit
    {
        public List<object> highlights { get; set; }
        public string index { get; set; }
        public string type { get; set; }
        public Result result { get; set; }
    }

    public class Response
    {
        public List<Hit> hits { get; set; }
    }

    public class RootObject
    {
        public Meta meta { get; set; }
        public Response response { get; set; }
    }
}

这是SearchGeniusASync方法的来源,以防它有用:

public async Task<string>SearchGeniusASync(string searchParameter)
{
    httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", clientAccessToken);

    var result = await httpClient.GetAsync(new Uri("https://api.genius.com/search?q=" + searchParameter), HttpCompletionOption.ResponseContentRead);
    var data = await result.Content.ReadAsStringAsync();

    return data;
}

这是我可以访问的范围:

https://i.imgur.com/9mZMvfp.png

以下是纯文本中的JSON请求示例:

https://pastebin.com/iA8dQafW

1 个答案:

答案 0 :(得分:1)

代码中不需要

GeniusApiObject,但我会留下它只是因为它有助于组织事物(可能是其他东西也有来自自动生成器的RootObject)。

问题是你试图反序列化为只是一个空类,类本身没有属性,所以你不能反序列化它。您需要反序列化为GeniusApiObject.RootObject

var data = JsonConvert.DeserializeObject<GeniusApiObject.RootObject>(test);

将反序列化为.RootObject子类。这已通过验证:

enter image description here

我正在使用File.ReadAllText("test.json")加载提供的示例API数据。

Here is a .NET Fiddle显示它正常工作(没有根对象,响应中只有一首歌)。感谢@maccttura