gpt4 book ai didi

C# JSON 对象不会反序列化

转载 作者:可可西里 更新时间:2023-11-01 11:54:21 25 4
gpt4 key购买 nike

所以我已经能够为一些事情获取 JSON 对象,但是这个对象要复杂得多。

我正在尝试从 Reddit 获取评论。这是我使用的方法:

    public async Task<List<string>> GetComments(string currentSubreddit, string topicID)
{
string commentUrl = "http://www.reddit.com/r/" + currentSubreddit + "/comments/" + topicID + "/.json";
List<Comments> commentList = new List<Comments>();
string jsonText = await wc.GetJsonText(commentUrl);

Comments.RootObject deserializeObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Comments.RootObject>(jsonText);

List<string> commentListTest = new List<string>();
//List<string> commentListTest = deserializeObject.data.children[0].data.children;
return commentListTest;

}

这是 GetJsonText 方法:

    public async Task<string> GetJsonText(string url)
{
var request = WebRequest.Create(url);
string text;
request.ContentType = "application/json; charset=utf-8";
var response = (HttpWebResponse)await request.GetResponseAsync();

using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}

return text;
}

这里是对象的链接:http://pastebin.com/WQ8XXGNA以及 jsonText 的链接:http://pastebin.com/7Kh6cA9a

返回的错误是这样说的:

'Newtonsoft.Json.JsonSerializationException' 类型的异常发生在 mscorlib.dll 中,但未在用户代码中处理

附加信息:无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“JuicyReddit.Comments+RootObject”,因为该类型需要一个 JSON 对象(例如 {"name":"value"})正确反序列化。

如果有人能帮助我找出问题所在,我将不胜感激。谢谢

最佳答案

实际上你的代码有一些问题

 public async Task<List<string>> GetComments(string currentSubreddit, string topicID)

你不需要在这里返回一个字符串列表,你需要返回一个完整的对象

首先将模型中的 RootObject 重命名为合适的名称,例如“CommentsObject”

像这样设置你的类并将其命名为 CommentsObject.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YOURNAMESPACE.Comments
{
public class MediaEmbed
{
}

public class SecureMediaEmbed
{
}

public class Data4
{
public int count { get; set; }
public string parent_id { get; set; }
public List<string> children { get; set; }
public string name { get; set; }
public string id { get; set; }
public string subreddit_id { get; set; }
public object banned_by { get; set; }
public string subreddit { get; set; }
public object likes { get; set; }
public object replies { get; set; }
public bool? saved { get; set; }
public int? gilded { get; set; }
public string author { get; set; }
public object approved_by { get; set; }
public string body { get; set; }
public object edited { get; set; }
public object author_flair_css_class { get; set; }
public int? downs { get; set; }
public string body_html { get; set; }
public string link_id { get; set; }
public bool? score_hidden { get; set; }
public double? created { get; set; }
public object author_flair_text { get; set; }
public double? created_utc { get; set; }
public object distinguished { get; set; }
public object num_reports { get; set; }
public int? ups { get; set; }
}

public class Child2
{
public string kind { get; set; }
public Data4 data { get; set; }
}

public class Data3
{
public string modhash { get; set; }
public List<Child2> children { get; set; }
public object after { get; set; }
public object before { get; set; }
}

public class Replies
{
public string kind { get; set; }
public Data3 data { get; set; }
}

public class Data2
{
public string domain { get; set; }
public object banned_by { get; set; }
public MediaEmbed media_embed { get; set; }
public string subreddit { get; set; }
public object selftext_html { get; set; }
public string selftext { get; set; }
public object likes { get; set; }
public object secure_media { get; set; }
public object link_flair_text { get; set; }
public string id { get; set; }
public SecureMediaEmbed secure_media_embed { get; set; }
public bool clicked { get; set; }
public bool stickied { 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 bool hidden { get; set; }
public string thumbnail { get; set; }
public string subreddit_id { get; set; }
public object 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 saved { get; set; }
public bool is_self { get; set; }
public string permalink { get; set; }
public string name { get; set; }
public double created { get; set; }
public string url { get; set; }
public object author_flair_text { get; set; }
public string title { get; set; }
public double created_utc { get; set; }
public int ups { get; set; }
public int num_comments { get; set; }
public bool visited { get; set; }
public object num_reports { get; set; }
public object distinguished { get; set; }
public Replies replies { get; set; }
public int? gilded { get; set; }
public string parent_id { get; set; }
public string body { get; set; }
public string body_html { get; set; }
public string link_id { get; set; }
public bool? score_hidden { get; set; }
public int? count { get; set; }
public List<string> children { get; set; }
}

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

public class Data
{
public string modhash { get; set; }
public List<Child> children { get; set; }
public object after { get; set; }
public object before { get; set; }
}

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

使您的命名空间正确!

然后处理请求并反序列化为评论对象列表:(如果你愿意,你可以使用 webclient 而不是 httpclient,这只是一个例子)

    private HttpClient client;

public async Task<List<CommentsObject>> GetComments()
{
client = new HttpClient();
var response = await client.GetAsync("http://www.reddit.com/r/AskReddit/comments/1ut6xc.json");
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
List<CommentsObject> comments = await JsonConvert.DeserializeObjectAsync<List<CommentsObject>>(json);
return comments;
}
else
{
throw new Exception("Errorhandling message");
}
}

关于C# JSON 对象不会反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21034528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com