c# - 如何将此 JSON 反序列化为 C# 对象
问题描述
我有以下 JSON 和类结构,但我无法弄清楚如何将其反序列化为对象。
这些类是由https://app.quicktype.io/从 josn 自动生成的
JSON 由 REST API 返回。
{
"last_executed_prices": {
"9707645": [
{
"contract_id": "33837223",
"last_executed_price": "79.37",
"timestamp": "2020-03-04T14:33:14.711040Z"
},
{
"contract_id": "33837246",
"last_executed_price": "3.85",
"timestamp": "2020-03-04T14:31:58.158066Z"
},
{
"contract_id": "33837219",
"last_executed_price": "36.5",
"timestamp": "2020-03-04T14:33:10.361513Z"
}
],
"999567": [
{
"contract_id": "33837223",
"last_executed_price": "79.37",
"timestamp": "2020-03-04T14:33:14.711040Z"
},
{
"contract_id": "33837246",
"last_executed_price": "3.85",
"timestamp": "2020-03-04T14:31:58.158066Z"
},
{
"contract_id": "33837248",
"last_executed_price": "7.69",
"timestamp": "2020-03-04T14:32:41.560315Z"
},
{
"contract_id": "33837220",
"last_executed_price": "4.17",
"timestamp": "2020-03-04T14:32:39.898192Z"
}
]
}
}
这是类结构:
public partial class LastExecutedPrices
{
public Dictionary<string, List<LastExecutedPrice>> LastExecutedPricesLastExecutedPrices { get; set; }
}
public partial class LastExecutedPrice
{
public long ContractId { get; set; }
public string LastExecutedPriceLastExecutedPrice { get; set; }
public DateTimeOffset Timestamp { get; set; }
}
我已经尝试了以下但只是得到一个空对象
LastExecutedPrices data = JsonConvert.DeserializeObject<LastExecutedPrices>(badjson);
解决方案
试试这个:
public partial class Root
{
[JsonProperty("last_executed_prices")]
public Dictionary<string, List<LastExecutedPrice>> LastExecutedPrices { get; set; }
}
public partial class LastExecutedPrice
{
[JsonProperty("contract_id")]
public long ContractId { get; set; }
[JsonProperty("last_executed_price")]
public string LastExecutedPriceLastExecutedPrice { get; set; }
[JsonProperty("timestamp")]
public DateTimeOffset Timestamp { get; set; }
}
您必须创建另一个类的原因是您的 JSON 是一个对象,其中包含一个名为 last_executed_prices 的字段,该字段是一个将字符串与 LastExecutedPrice 列表相关联的字典。
对于 ContractId 属性,您应该将其更改为字符串类型,因为在您的 JSON 中它被双引号包围,因此它将被反序列化为字符串。如果要将其转换为 int,则必须添加转换器。
更新:您也可以声明 ContractId 属性。如果该字符串是可以解析为 Int64 的有效数字,Newtonsoft.JSON 能够将 JSON 中的字符串反序列化为 long。
您可以通过添加以下内容轻松测试我的解决方案:
string json = @"{
'last_executed_prices': {
'9707645': [
{
'contract_id': '33837223',
'last_executed_price': '79.37',
'timestamp': '2020-03-04T14:33:14.711040Z'
},
{
'contract_id': '33837246',
'last_executed_price': '3.85',
'timestamp': '2020-03-04T14:31:58.158066Z'
},
{
'contract_id': '33837219',
'last_executed_price': '36.5',
'timestamp': '2020-03-04T14:33:10.361513Z'
}
],
'999567': [
{
'contract_id': '33837223',
'last_executed_price': '79.37',
'timestamp': '2020-03-04T14:33:14.711040Z'
},
{
'contract_id': '33837246',
'last_executed_price': '3.85',
'timestamp': '2020-03-04T14:31:58.158066Z'
},
{
'contract_id': '33837248',
'last_executed_price': '7.69',
'timestamp': '2020-03-04T14:32:41.560315Z'
},
{
'contract_id': '33837220',
'last_executed_price': '4.17',
'timestamp': '2020-03-04T14:32:39.898192Z'
}
]
}
}";
Root obj = JsonConvert.DeserializeObject<Root>(json);
反序列化后,您应该有一个有效的对象,其中包含将字符串关联到 LastExecutedPrice 列表的字典。
我希望我的解决方案有用,如果您有任何疑问,请告诉我。
推荐阅读
- sql - 从具有一些共同列的多个表创建视图
- python - 尾随日志文件的 Python 脚本
- python - 有没有办法将使用“fileConfig”读取的日志配置格式转换为“dictConfig”所需的格式?
- javascript - 捕获第一次向用户请求地理位置
- reactjs - 你如何区分 FontAwesome 的圈子?
- typescript - ramda 中的打字稿突然出错
- c# - 如何在 Startup.cs 中调用存储库?
- regex - 在特定文本之后查找日期(oracle)
- json - 如何将数据帧输出转换为 json 格式,然后规范化数据?
- reactjs - 如何使用反应将链接存储到变量中?