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
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)