【发布时间】:2018-04-24 22:07:06
【问题描述】:
我正在尝试反序列化这个 json:
{"name":"FC Internazionale Milano","y":55},{"name":"AC Chievo Verona","y":45,"sliced":true}
在包含在字符串中的函数内部可用:
window.addEvent('domready', function() {
var chart = new PieChart('Possession');
chart.addSeries('Possession', [{"name":"FC Internazionale Milano","y":55},{"name":"AC Chievo Verona","y":45,"sliced":true}]);
chart.highChartsOptions.plotOptions.pie.topmargin = '20';
chart.highChartsOptions.plotOptions.pie.size = "80%";
chart.highChartsOptions.plotOptions.pie.center = ['50%', '55%'];
chart.render('page_chart_1_chart_statsplus_1_chart_possession_1-wrapper');
});
对于get json,我使用了这个正则表达式:
System.Text.RegularExpressions.Regex.Match(script, @"Possession[^\{]+(.*})").Groups[1].Value;
返回上面的json。于是我就这样反序列化了:
var json = JsonConvert.DeserializeObject<JsonMatchStat>(jsonStr);
JsonMatchStat的结构是这样的:
public class JsonMatchStat
{
public string name { get; set; }
public int y { get; set; }
public bool? sliced { get; set; }
}
当我反序列化我得到的 json 时:
'读完JSON内容后遇到的附加文字:,.路径'',第 1 行,位置 42。'
我做错了什么?
【问题讨论】:
-
这不是有效的 JSON,它看起来像多个 JSON 文件用逗号拼接在一起。
-
我不是 JSON 专家,但该 JSON 字符串中有两个类。是这个问题吗?
-
这些是您想要反序列化为一个的两个对象,将其设为 [{"name":"FC Internazionale Milano","y":55},{"name":"AC Chievo Verona","y":45,"sliced":true}] 并反序列化为列表 var json = JsonConvert.DeserializeObject
- >(jsonStr);
-
@fantaghiro,看来你已经有了答案
-
实际上,Json.NET 11.0.1 原生支持读取逗号分隔的 JSON 对象序列。见How to deserialize dodgy JSON (with improperly quoted strings, and missing brackets)?。您需要设置
JsonReader.SupportMultipleContent = true。