-1

I have a problem with json deserialization

JSON string

{
  "id": "1",
  "marketId": "1",
  "userId": "2",
  "svisorId": "1",
  "orders": "[{\"Id\": 3, \"Count\": 1, \"Price\": 1000, \"Weight\": 1500, \"ShareId\": 0}]",
  "date": "1516770391",
  "totalWeight": "1500",
  "totalPrice": "1000",
  "debt": "1000",
  "shipped": "0"
}

"orders" property is an array of class ProductOrderInfo

Classes

public class OrderInfo
{
    public int Id
    { get; set; }
    public int MarketId
    { get; set; }
    public int UserId
    { get; set; }
    public int SVisorId
    { get; set; }
    //public List<ProductOrderInfo> Orders; even tried List type
    public ProductOrderInfo[] Orders
    { get; set; }
    public int TotalPrice
    { get; set; }
    public int TotalWeight
    { get; set; }
    public int Dept
    { get; set; }
    public bool Shipped
    { get; set; }
    public int Date
    { get; set; }
}

public class ProductOrderInfo
    {
        public int Id
        { get; set; }
        public int Count
        { get; set; }
        public int Price
        { get; set; }
        public int Weight
        { get; set; }
        public int ShareId
        { get; set; }
    }

Deserialization with JsonConvert.DeserializeObject(json_string) always fails and throws an exception

JsonSerializationException: Error converting value "[{"Id": 3, "Count": 1, "Price": 1000, "Weight": 1500, "ShareId": 0}]" to type 'Project.Classes.ProductOrderInfo[]'. Path 'orders', line 6, position 92.

I've already searched for a similiar problem like this C# JSON Deserialization Error Array but it didn't help me

2
  • 7
    Your problem is that "orders" in the JSON is a string, not an array. It is doubly-serialized. It would have worked with "orders" : [{"id":1, ... }],. It is visibly serialized with a different tool, by the way, with PascalCase vs. camelCase. Commented Jan 24, 2018 at 5:39
  • Didn't notice that it's string. Thanks for help Commented Jan 24, 2018 at 6:19

1 Answer 1

0

Change your JSON string as below

{
  "id": "1",
  "marketId": "1",
  "userId": "2",
  "svisorId": "1",
  "orders": [{"Id": 3, "Count": 1, "Price": 1000, "Weight": 1500, "ShareId":0}],
  "date": "1516770391",
  "totalWeight": "1500",
  "totalPrice": "1000",
  "debt": "1000",
  "shipped": 0
}

Error is due to the
you consider Array of orders as string and shipped is bool but consider it as a string.

Use below code for deserialization

JsonConvert.DeserializeObject<OrderInfo>(json_string)
Sign up to request clarification or add additional context in comments.

3 Comments

I've done these string json_string = tkn.ToString().Replace("\"[", "[").Replace("]\"", "]").Replace("\\", ""); lst.Add(JsonConvert.DeserializeObject<OrderInfo>(json_string)); Also changed Boolean "shipped" to integer and everything is ok now. Thanks
Also change "shipped": "0" to "shipped": 0. And you can also upvote on answer :).
No need to change the json. String values converts to integer very well. Also I can not upvote the answer as I don't have enough rights

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.