【发布时间】:2014-01-28 20:21:41
【问题描述】:
所以我已经能够为一些事情获取 JSON 对象,但是这个对象要复杂得多。
我正在尝试从 Reddit 获取 cmets。 这是我使用的方法:
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"})正确反序列化。
如果有人能帮助我找出问题所在,我将不胜感激。 谢谢
【问题讨论】:
-
你从哪里得到这个 json 数据?认为您发布的 json 字符串无效。
-
@Jim 我得到这个 url 并在 GetJsonText www.reddit.com/r/AskReddit/cmets/1ut6xc/.json 中使用它
-
这里解释了额外的错误stackoverflow.com/questions/17762032/…
-
@Jim 你知道如何在我使用 Newtonsoft 的情况下做到这一点吗?仍然不确定如何解决它。
-
我为你添加了示例代码
标签: c# json windows visual-studio-2010 serialization