【发布时间】:2019-11-20 15:52:04
【问题描述】:
需要使用 newtonsoft.json.net 反序列化这个 JSON:
{
"dataset": {
"id": 36592406,
"dataset_code": "EON_X",
"database_code": "FSE",
"start_date": "2019-11-18",
"end_date": "2019-11-18",
"data": [
[
"2019-11-18",
9.17,
9.204,
9.121,
9.167,
null,
6088172.0,
55793844.0,
null,
null,
null
]
]
}
}
到List<FseItem>。问题是我只需要“数据”部分。我试图用这个:
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
var json = GetResult(url);
var obj = JsonConvert.DeserializeObject<List<FseItem>>(json, settings);
这是我的 FseItem 对象:
public class FseItem
{
public DateTime Date { get; set; }
public decimal Open { get; set; }
public decimal High { get; set; }
public decimal Low { get; set; }
public decimal Close { get; set; }
public decimal Change { get; set; }
public decimal TradedVolume { get; set; }
public decimal TurnOver { get; set; }
public decimal LastPrice { get; set; }
public decimal TradedUnits { get; set; }
public decimal DailyTurnover { get; set; }
}
但我收到此错误:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[InvestorWeb2.WebUI.Domain.QwertyReporting.FseReport+FseItem]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
我从 Quandl 免费数据库中获取我的 JSON,因此我无法更改 json 数据中的任何内容。在此示例中,json 仅包含一个元素,但我也将使用大型 json 文件。我的代码有什么问题,我应该怎么做才能解决它们?
对于大量数据的快速解决方案以及关于解析和使用大 json 的一些建议将不胜感激!
【问题讨论】:
-
这甚至是有效的 json 吗? JSONLint 说它不是...
-
所有不同的 json 规范都失败了,它是无效的
json。由于您是从另一个地方获取数据,我会让他们知道这是错误的并且需要修复。如果您从底部的],中删除,,它将通过rfc 8259规范。 -
只有部分 JSON 文本表示一个数组,但您的代码将整个内容视为一个集合/数组。 Start your research here
-
请检查您的 json,因为它无效,我想您忘记删除逗号了。
-
我的错,我删除了一些数据以使我的示例更简单。我编辑了json,所以它是有效的atm。但是在反序列化时尝试获取对象列表时仍然出错
标签: c# json serialization