将JSON数组反序列化为C#

时间:2017-01-29 11:24:45

标签: c# .net arrays json

我正在尝试查询Reddit API并循环遍历线程的答案以获得我想要的答案。但是,当我查询api(https://www.reddit.com/comments/2pfyg8.json?sort=top作为示例)时,我将获得一个带有两个对象的json数组。我想循环对象2,因为这是包含所有实际注释的对象,第一个对象是线程本身。

这似乎是C#中的挑战,或者对我来说至少是一个挑战。我使用JSON.NETNewtonsoft.Json来完成此操作,这是我到目前为止所做的:

var commentPath = $"http://www.reddit.com/comments/{questionId}.json?sort=top";
HttpResponseMessage commentResponse = await client.GetAsync(commentPath);
var commentJson = await response.Content.ReadAsStringAsync();
var answers = JsonConvert.DeserializeObject<dynamic>(commentJson);

int commentCount = 0;
foreach (var answerContainer in answers[1].data.children) { }

我还尝试使用http://json2csharp.com/为我生成正确的类型而不是dynamic,但它似乎也不正确。

这是我收到的错误:

 System.ArgumentException: Accessed JObject values with invalid key value: 1. Object property name expected. vid Newtonsoft.Json.Linq.JObject.get_Item(Object key) vid CallSite.Target(Closure , CallSite , Object , Int32 )

如果有人能够帮助我获得我正在寻找的评论,我会很高兴。

1 个答案:

答案 0 :(得分:1)

基于Reddit的JSON响应,我生成了这些类(在Visual Studio中将JSON粘贴为Classes函数):

public class reddit
{
    public string kind { get; set; }
    public Data data { get; set; }
}

public class Data
{
    public string modhash { get; set; }
    public Child[] children { get; set; }
    public object after { get; set; }
    public object before { get; set; }
}

public class Child
{
    public string kind { get; set; }
    public Data1 data { get; set; }
}

public class Data1
{
    public bool contest_mode { get; set; }
    public object banned_by { get; set; }
    public Media_Embed media_embed { get; set; }
    public string subreddit { get; set; }
    public string selftext_html { get; set; }
    public string selftext { get; set; }
    public object likes { get; set; }
    public object suggested_sort { get; set; }
    public object[] user_reports { get; set; }
    public object secure_media { get; set; }
    public bool saved { get; set; }
    public string id { get; set; }
    public int gilded { get; set; }
    public Secure_Media_Embed secure_media_embed { get; set; }
    public bool clicked { get; set; }
    public object report_reasons { get; set; }
    public string author { get; set; }
    public object media { get; set; }
    public int score { get; set; }
    public object approved_by { get; set; }
    public bool over_18 { get; set; }
    public string domain { get; set; }
    public bool hidden { get; set; }
    public int num_comments { get; set; }
    public string thumbnail { get; set; }
    public string subreddit_id { get; set; }
    public bool edited { get; set; }
    public object link_flair_css_class { get; set; }
    public object author_flair_css_class { get; set; }
    public int downs { get; set; }
    public bool archived { get; set; }
    public object removal_reason { get; set; }
    public bool stickied { get; set; }
    public bool is_self { get; set; }
    public bool hide_score { get; set; }
    public bool spoiler { get; set; }
    public string permalink { get; set; }
    public bool locked { get; set; }
    public string name { get; set; }
    public float created { get; set; }
    public string url { get; set; }
    public object author_flair_text { get; set; }
    public bool quarantine { get; set; }
    public string title { get; set; }
    public float created_utc { get; set; }
    public object link_flair_text { get; set; }
    public int ups { get; set; }
    public float upvote_ratio { get; set; }
    public object[] mod_reports { get; set; }
    public bool visited { get; set; }
    public object num_reports { get; set; }
    public object distinguished { get; set; }
    public string link_id { get; set; }
    public object replies { get; set; }
    public string parent_id { get; set; }
    public int controversiality { get; set; }
    public string body { get; set; }
    public string body_html { get; set; }
    public bool score_hidden { get; set; }
}

public class Media_Embed
{
}

public class Secure_Media_Embed
{
}

然后,工作代码看起来像这样:

using (var client = new HttpClient())
        {
            var commentPath = $"http://www.reddit.com/comments/{questionId}.json?sort=top";
            HttpResponseMessage commentResponse = client.GetAsync(commentPath).Result;
            var commentJson = commentResponse.Content.ReadAsStringAsync().Result;
            var answers = JsonConvert.DeserializeObject<reddit[]>(commentJson);

            int commentCount = 0;
            foreach (var answerContainer in answers[1].data.children)
            {
            }
        }