Json反序列化解析非有效的Json对象

时间:2017-03-07 13:27:22

标签: c# .net json api deserialization

我正在尝试将从Web API恢复的A Json对象反序列化为强类型对象列表,如下所示:

WebClient wc = new WebClient();

import plotly

fig = {
    'data': [{'labels': ['Residential', 'Non-Residential', 'Utility'],
              'values': [19, 26, 55],
              'type': 'pie',
              'sort': False,
              'marker': {'colors': ['rgb(255, 0, 0)',
                                    'rgb(0, 255, 0)',
                                    'rgb(0, 0, 255)']
                        }
            }]
     }

fig = {
    'data': [{'labels': ['Residential', 'Non-Residential', 'Utility'],
              'values': [100, 10, 25],
              'type': 'pie',
              'sort': False,
              'marker': {'colors': ['rgb(255, 0, 0)',
                                    'rgb(0, 255, 0)',
                                    'rgb(0, 0, 255)']
                        }
            }]
     }
plotly.offline.plot(fig)

问题是我总是将结果作为1个字符串,因为解析器不解析有效的Json对象。 (我猜它是无效的字符,无法正确解析)...

有没有人有任何想法?

3 个答案:

答案 0 :(得分:1)

您需要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);

测试:

enter image description here

答案 1 :(得分:0)

您还可以尝试使用Dictionary<string, IEnumerable<Dictionary<string, object>>>

var root = JsonConvert.DeserializeObject<Dictionary<string, IEnumerable<Dictionary<string, object>>>>(jsonMain);
List<News> news = root["d"].Select(result => new News()
{
   id = result["ID"] as string,
   Title = result["Title"] as string,
   Date = result["PublishingDate"] as string,
   Content = result["News"] as string
}).ToList();
return View(news);

答案 2 :(得分:0)

可以替代

class News
{

    [JsonProperty("ID")]
    public int id { get; set; }

    [JsonProperty("Title")]
    public string Title { get; set; }

    [JsonProperty("PublishingDate")]
    public DateTime Date { get; set; }

    [JsonProperty("News")]
    public string Content { get; set; }
}

WebClient实现IDisposible接口。在using statement中使用它并不错。

        using (WebClient wc = new WebClient())
        {
            var jsonMain = wc.DownloadString("http://api.flexianalysis.com/services/FlexiAnalysisService.svc/FlexiAnalysisNews?key=gZ_lhbJ_46ThmvEki2lF&catagory=Forex");
            JObject token2 = JObject.Parse(jsonMain);
            string s = token2["d"].ToString();
            List<News> list = JArray.Parse(s).ToObject<List<News>>();
        }