在 C# 中将 json 反序列化为 List<object>
Deserialize json to List<object> in C#
我有以下 JSON 字符串
{
"data": [
{
"symbol": "1COV.GE",
"exposure": "0",
"makerExposure": "-2028",
"takerExposure": "2028",
"makerPnl": "447.6688",
"takerPnl": "-447.6688",
"makerPositions": [
{
"name": "IB_001",
"position": "-2028",
"vwap": "47.41",
"pnl": "447.6688"
}
],
"takerPositions": [
{
"name": "MT5_1",
"position": "2028",
"vwap": "47.41",
"pnl": "-447.6688"
}
]
},
{
"symbol": "A",
"exposure": "0",
"makerExposure": "-10",
"takerExposure": "10",
"makerPnl": "-4.6",
"takerPnl": "4.6",
"makerPositions": [
{
"name": "IB_002",
"position": "-10",
"vwap": "136.78",
"pnl": "-4.6"
}
],
"takerPositions": [
{
"name": "MT5_1",
"position": "10",
"vwap": "136.78",
"pnl": "4.6"
}
],
"total": 2
}
}
我的目标是将其序列化为节点“数据”中的对象列表:
我有 类 映射数据节点字段:
public class Positions
{
public string name { get; set; }
public string position { get; set; }
public string vwap { get; set; }
public string pnl { get; set; }
}
public class ExPositions
{
public string symbol { get; set; }
public string exposure { get; set; }
public string makerExposure { get; set; }
public string takerExposure { get; set; }
public string makerPnl { get; set; }
public string takerPnl { get; set; }
public OZPositions makerPositions { get; set; }
public OZPositions takerPositions { get; set; }
}
您是否知道如何将节点“数据”转换为“Expositions”对象列表,例如。列表
我已经这样做了,但到目前为止它会引发错误
var positions = JsonSerializer.Deserialize<ExPositions>(json_string);
试试:
public class Positions
{
public string name { get; set; }
public string position { get; set; }
public string vwap { get; set; }
public string pnl { get; set; }
}
public class ExPositions
{
public string symbol { get; set; }
public string exposure { get; set; }
public string makerExposure { get; set; }
public string takerExposure { get; set; }
public string makerPnl { get; set; }
public string takerPnl { get; set; }
public Positions makerPositions { get; set; }
public Positions takerPositions { get; set; }
}
public class YourResult{
public ExPositions data { get; set; }
public int total { get; set; }
}
然后调用:
var positions = JsonSerializer.Deserialize<YourResult>(json_string);
您的 json 中有一个错误 - 它缺少数组的结尾 ]
(我假设这是一个拼写错误)。
真正的问题是你需要一个包装器 class 来表示 json 的 data
节点,它应该包含 ExPositions
的列表(或数组) . makerPositions
和 takerPositions
也应该成为列表(或数组)。添加以下 class 并更新 ExPositions
的位置属性:
public class Data
{
public List<ExPositions> data { get; set; }
}
// change positions to use a List too
public class ExPositions
{
...
public List<Positions> makerPositions { get; set; }
public List<Positions> takerPositions { get; set; }
}
然后你可以反序列化使用:
var result = JsonSerializer.Deserialize<Data>(json);
不清楚 ""total"": 2
属性 在模型中的位置(由于我提到的问题,json 中不清楚),您可以将其添加到Data
class 以上(如果它属于那里)。
如haldo mentioned, there is a typo in your JSON. To quickly parse and validate your JSON data, you can use any online JSON parsers to validate your JSON data. I usually use the chrome extension JSON Viewer Pro.
此外,在 haldo 为演示提供给 .NET Fiddle 的 link 中,JSON 数据中有一个尾随逗号,JSON 反序列化程序可能不要忽略。
这是 haldo 提供的已编辑演示的 link。
Edited Demo
我有以下 JSON 字符串
{
"data": [
{
"symbol": "1COV.GE",
"exposure": "0",
"makerExposure": "-2028",
"takerExposure": "2028",
"makerPnl": "447.6688",
"takerPnl": "-447.6688",
"makerPositions": [
{
"name": "IB_001",
"position": "-2028",
"vwap": "47.41",
"pnl": "447.6688"
}
],
"takerPositions": [
{
"name": "MT5_1",
"position": "2028",
"vwap": "47.41",
"pnl": "-447.6688"
}
]
},
{
"symbol": "A",
"exposure": "0",
"makerExposure": "-10",
"takerExposure": "10",
"makerPnl": "-4.6",
"takerPnl": "4.6",
"makerPositions": [
{
"name": "IB_002",
"position": "-10",
"vwap": "136.78",
"pnl": "-4.6"
}
],
"takerPositions": [
{
"name": "MT5_1",
"position": "10",
"vwap": "136.78",
"pnl": "4.6"
}
],
"total": 2
}
}
我的目标是将其序列化为节点“数据”中的对象列表: 我有 类 映射数据节点字段:
public class Positions
{
public string name { get; set; }
public string position { get; set; }
public string vwap { get; set; }
public string pnl { get; set; }
}
public class ExPositions
{
public string symbol { get; set; }
public string exposure { get; set; }
public string makerExposure { get; set; }
public string takerExposure { get; set; }
public string makerPnl { get; set; }
public string takerPnl { get; set; }
public OZPositions makerPositions { get; set; }
public OZPositions takerPositions { get; set; }
}
您是否知道如何将节点“数据”转换为“Expositions”对象列表,例如。列表
我已经这样做了,但到目前为止它会引发错误
var positions = JsonSerializer.Deserialize<ExPositions>(json_string);
试试:
public class Positions
{
public string name { get; set; }
public string position { get; set; }
public string vwap { get; set; }
public string pnl { get; set; }
}
public class ExPositions
{
public string symbol { get; set; }
public string exposure { get; set; }
public string makerExposure { get; set; }
public string takerExposure { get; set; }
public string makerPnl { get; set; }
public string takerPnl { get; set; }
public Positions makerPositions { get; set; }
public Positions takerPositions { get; set; }
}
public class YourResult{
public ExPositions data { get; set; }
public int total { get; set; }
}
然后调用:
var positions = JsonSerializer.Deserialize<YourResult>(json_string);
您的 json 中有一个错误 - 它缺少数组的结尾 ]
(我假设这是一个拼写错误)。
真正的问题是你需要一个包装器 class 来表示 json 的 data
节点,它应该包含 ExPositions
的列表(或数组) . makerPositions
和 takerPositions
也应该成为列表(或数组)。添加以下 class 并更新 ExPositions
的位置属性:
public class Data
{
public List<ExPositions> data { get; set; }
}
// change positions to use a List too
public class ExPositions
{
...
public List<Positions> makerPositions { get; set; }
public List<Positions> takerPositions { get; set; }
}
然后你可以反序列化使用:
var result = JsonSerializer.Deserialize<Data>(json);
不清楚 ""total"": 2
属性 在模型中的位置(由于我提到的问题,json 中不清楚),您可以将其添加到Data
class 以上(如果它属于那里)。
如haldo mentioned, there is a typo in your JSON. To quickly parse and validate your JSON data, you can use any online JSON parsers to validate your JSON data. I usually use the chrome extension JSON Viewer Pro.
此外,在 haldo 为演示提供给 .NET Fiddle 的 link 中,JSON 数据中有一个尾随逗号,JSON 反序列化程序可能不要忽略。
这是 haldo 提供的已编辑演示的 link。 Edited Demo