Json反序列化解析无效的Json对象 [英] Json Deserialization parsing non valid Json object
本文介绍了Json反序列化解析无效的Json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试反序列化从Web API检索到的Json对象到强类型对象列表中,如下所示:
I'm trying to deserialize A Json object retrived from Web API into list of strong type objects as follows :
WebClient wc = new WebClient();
WebClient wc = new WebClient();
// Downloading & Deserializing the Json file
var jsonMain = wc.DownloadString("http://api.flexianalysis.com/services/FlexiAnalysisService.svc/FlexiAnalysisNews?key=___&catagory=Forex");
JObject token2 = JObject.Parse(jsonMain);
List<News> listNews = new List<News>();
foreach (var result in token2["d"])
{
news = new News();
news.id = (string)result["ID"];
news.Title = (string)result["Title"];
news.Date = (string)result["PublishingDate"];
news.Content = (string)result["News"];
listNews.Add(news);
}
return View(listNews);
问题是我总是以1个字符串的形式获取结果,因为解析器无法解析有效的Json对象. (我想这是无效字符,无法正确解析)...
The problem is that I always get the result as 1 string, Because the parser does not parse a valid Json object. (I guess it get's to invalid character and can't get it parsed correctly)...
有人有什么想法吗?
推荐答案
您需要JArray results = JArray.Parse(token2["d"].ToString());
请尝试
WebClient wc = new WebClient();
// Downloading & Deserializing the Json file
var jsonMain = wc.DownloadString("http://api.flexianalysis.com/services/FlexiAnalysisService.svc/FlexiAnalysisNews?key=gZ_lhbJ_46ThmvEki2lF&catagory=Forex");
JObject token2 = JObject.Parse(jsonMain);
JArray results = JArray.Parse(token2["d"].ToString());
List<News> listNews = new List<News>();
foreach (var result in results)
{
news = new News();
news.id = (string)result["ID"];
news.Title = (string)result["Title"];
news.Date = (string)result["PublishingDate"];
news.Content = (string)result["News"];
listNews.Add(news);
}
return View(listNews);
测试:
这篇关于Json反序列化解析无效的Json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文