Vũ Hoàng
Vũ Hoàng

Reputation: 265

Deserialize Json to Object Exception

I have a json string:

{"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
                            }
                        }
                    ]
            }
        }
      ]
    }
}
}

And I have objects

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; }
}

So when i use code:

var objectValue = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(jsonString);

var objectDeserialize = JsonConvert.DeserializeObject<OrderListResponse>(objectValue.Values.First()["orderListResponse"].ToString());

I get an error message:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[LegDetail]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

Can someone show me the right code for it to work?

Upvotes: 2

Views: 769

Answers (2)

Jenish Rabadiya
Jenish Rabadiya

Reputation: 6766

logdetails in the first order is not an array...

"legDetails":
 {
    "legNumber":1,
    "symbolInfo":
     {
       "symbol":"CSCO"
     }
 }

should be like following:

"legDetails":
 [{
    "legNumber":1,
    "symbolInfo":
     {
       "symbol":"CSCO"
     }
 }]

Update:

So now the problem is you want to deserialize a json that can sometimes be array and sometimes may represent an object.

Look at another stackoverflow Thread : Deserializing JSON when sometimes array and sometimes object

This might help you to resolve the issue.

Upvotes: 1

a.azemia
a.azemia

Reputation: 304

Your JSON is invalid. It should be:

{
   "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
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

Upvotes: 0

Related Questions