将 Json 反序列化为对象异常
Deserialize Json to Object Exception
我有一个 json 字符串:
{"GetOrderListResponse":
{"orderListResponse":
{
"orderDetails":
[
{"order":
{
"orderId":208,
"legDetails":
{
"legNumber":1,
"symbolInfo":
{
"symbol":"CSCO"
}
}
}
},
{"order":
{
"orderId":200,
"legDetails":
[
{
"legNumber":1,
"symbolInfo":
{
"symbol":"IBM"
}
},
{
"legNumber":2,
"symbolInfo":
{
"symbol":"IBM",
"callPut":"CALL",
"expYear":2010,
"expMonth":4,
"expDay":17,
"strikePrice":115
}
}
]
}
}
]
}
}
}
而且我有对象
public class OrderListResponse
{
public List<OrderDetail> orderDetails { get; set; }
}
public class OrderDetail
{
public Order order { get; set; }
}
public class Order
{
public long orderId { get; set; }
public List<LegDetail> legDetails { get; set; }
}
public class LegDetail
{
public long legNumber { get; set; }
public Symbol symbolInfo { get; set; }
}
public class Symbol
{
public string symbol { get; set; }
public string callPut { get; set; }
public int expYear { get; set; }
public int expMonth { get; set; }
public int expDay { get; set; }
public int strikePrice { get; set; }
}
所以当我使用代码时:
var objectValue = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(jsonString);
var objectDeserialize = JsonConvert.DeserializeObject<OrderListResponse>(objectValue.Values.First()["orderListResponse"].ToString());
我收到一条错误消息:
无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[LegDetail]',因为该类型需要一个 JSON 数组(例如 [1,2,3]) 以正确反序列化。
有人可以告诉我正确的代码吗?
第一个顺序的logdetails不是一个数组...
"legDetails":
{
"legNumber":1,
"symbolInfo":
{
"symbol":"CSCO"
}
}
应该如下所示:
"legDetails":
[{
"legNumber":1,
"symbolInfo":
{
"symbol":"CSCO"
}
}]
更新:
所以现在的问题是你想反序列化一个 json,它有时可以是数组,有时可以代表一个对象。
查看另一个 Whosebug 线程:Deserializing JSON when sometimes array and sometimes object
这可能会帮助您解决问题。
您的JSON无效。应该是:
{
"GetOrderListResponse":{
"orderListResponse":{
"orderDetails":[
{
"order":{
"orderId":208,
"legDetails":[
{
"legNumber":1,
"symbolInfo":{
"symbol":"CSCO"
}
}
]
}
},
{
"order":{
"orderId":200,
"legDetails":[
{
"legNumber":1,
"symbolInfo":{
"symbol":"IBM"
}
},
{
"legNumber":2,
"symbolInfo":{
"symbol":"IBM",
"callPut":"CALL",
"expYear":2010,
"expMonth":4,
"expDay":17,
"strikePrice":115
}
}
]
}
}
]
}
}
}
我有一个 json 字符串:
{"GetOrderListResponse":
{"orderListResponse":
{
"orderDetails":
[
{"order":
{
"orderId":208,
"legDetails":
{
"legNumber":1,
"symbolInfo":
{
"symbol":"CSCO"
}
}
}
},
{"order":
{
"orderId":200,
"legDetails":
[
{
"legNumber":1,
"symbolInfo":
{
"symbol":"IBM"
}
},
{
"legNumber":2,
"symbolInfo":
{
"symbol":"IBM",
"callPut":"CALL",
"expYear":2010,
"expMonth":4,
"expDay":17,
"strikePrice":115
}
}
]
}
}
]
}
}
}
而且我有对象
public class OrderListResponse
{
public List<OrderDetail> orderDetails { get; set; }
}
public class OrderDetail
{
public Order order { get; set; }
}
public class Order
{
public long orderId { get; set; }
public List<LegDetail> legDetails { get; set; }
}
public class LegDetail
{
public long legNumber { get; set; }
public Symbol symbolInfo { get; set; }
}
public class Symbol
{
public string symbol { get; set; }
public string callPut { get; set; }
public int expYear { get; set; }
public int expMonth { get; set; }
public int expDay { get; set; }
public int strikePrice { get; set; }
}
所以当我使用代码时:
var objectValue = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(jsonString);
var objectDeserialize = JsonConvert.DeserializeObject<OrderListResponse>(objectValue.Values.First()["orderListResponse"].ToString());
我收到一条错误消息:
无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[LegDetail]',因为该类型需要一个 JSON 数组(例如 [1,2,3]) 以正确反序列化。
有人可以告诉我正确的代码吗?
第一个顺序的logdetails不是一个数组...
"legDetails":
{
"legNumber":1,
"symbolInfo":
{
"symbol":"CSCO"
}
}
应该如下所示:
"legDetails":
[{
"legNumber":1,
"symbolInfo":
{
"symbol":"CSCO"
}
}]
更新:
所以现在的问题是你想反序列化一个 json,它有时可以是数组,有时可以代表一个对象。
查看另一个 Whosebug 线程:Deserializing JSON when sometimes array and sometimes object
这可能会帮助您解决问题。
您的JSON无效。应该是:
{
"GetOrderListResponse":{
"orderListResponse":{
"orderDetails":[
{
"order":{
"orderId":208,
"legDetails":[
{
"legNumber":1,
"symbolInfo":{
"symbol":"CSCO"
}
}
]
}
},
{
"order":{
"orderId":200,
"legDetails":[
{
"legNumber":1,
"symbolInfo":{
"symbol":"IBM"
}
},
{
"legNumber":2,
"symbolInfo":{
"symbol":"IBM",
"callPut":"CALL",
"expYear":2010,
"expMonth":4,
"expDay":17,
"strikePrice":115
}
}
]
}
}
]
}
}
}