[英]Deserialize a JSON array into C#
我正在嘗試查詢Reddit API並遍歷一個線程的答案以獲得我想要的答案。 但是,當我查詢api時(以https://www.reddit.com/comments/2pfyg8.json?sort=top為例),我將獲得帶有兩個對象的json數組。 我想遍歷對象2,因為這是具有所有實際注釋的對象,而第一個對象是線程本身。
這似乎在C#中是一個挑戰,或者對我來說至少是一個挑戰。 我使用JSON.NET
或Newtonsoft.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 )
如果有人能夠幫助我獲得我想要的評論,我將感到非常高興。
基於Reddit的JSON響應,我生成了此類(在Visual Studio中將JSON作為類粘貼):
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)
{
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.