I'm using https://jsonutils.com/ to build C# classes from JSON but I'm stuck for the correct class for variationsChoices. The part of the JSON file I'm having problems with is:
{"orderItems": [
{
"itemId": "92953385-4ce1-44be-8a20-c3cc597dd4a7",
"price": 325,
"count": 1
},
{
"itemId": "dd757994-1480-4450-ae47-6d7f87dbcc33",
"price": 325,
"count": 1
},
{
"itemId": "912f7bbb-58e0-45cc-a7ec-f35987073941",
"price": 70,
"count": 2
},
{
"itemId": "75cb81b8-22fb-4a65-bcf8-17e0e7d41f88",
"variations": [
{
"title": {
"en_GB": "Spiced or plain"
},
"itemIds": [
"38319b13-eabc-4777-be55-b9d9b5e8be3b",
"997f4cd7-3e8c-4549-97cb-de8b26bdb304"
],
"minNumAllowed": 1,
"maxNumAllowed": 1,
"displayType": "choice"
}
],
"variationsChoices": [
[
{
"itemId": "997f4cd7-3e8c-4549-97cb-de8b26bdb304",
"count": 1
}
]
],
"price": 70,
"count": 3
},
{
"itemId": "ee3f9e6f-b7e1-4740-b837-1d81692c60e9",
"price": 1495,
"count": 1
},
{
"itemId": "d124f18a-274d-4bb4-b554-d810c1145462",
"price": 280,
"count": 1
},
{
"itemId": "81253f1b-d8e2-4106-aebe-29e6298aba37",
"price": 270,
"count": 1
},
{
"itemId": "4f677d74-7c5a-416b-b486-2e1bf3f170a1",
"price": 325,
"count": 1
},
{
"itemId": "52229604-1829-4148-92be-930eff07bef5",
"variations": [
{
"title": {
"en_GB": "Option"
},
"itemIds": [
"1230916c-3d2d-4a4b-9aff-55815924e13f",
"86bef61f-a59f-4c1f-88a6-009594ed1f54"
],
"minNumAllowed": 1,
"maxNumAllowed": 1,
"displayType": "choice"
}
],
"variationsChoices": [
[
{
"itemId": "86bef61f-a59f-4c1f-88a6-009594ed1f54",
"count": 1
}
]
],
"price": 995,
"count": 1
}
]
}
jsonutils returns the following classes but public IList<IList<>> variationsChoices { get; set; }is not value C#
public class Title
{
public string en_GB { get; set; }
}
public class Variation
{
public Title title { get; set; }
public IList<string> itemIds { get; set; }
public int minNumAllowed { get; set; }
public int maxNumAllowed { get; set; }
public string displayType { get; set; }
}
public class OrderItem
{
public string itemId { get; set; }
public int price { get; set; }
public int count { get; set; }
public IList<Variation> variations { get; set; }
public IList<IList<>> variationsChoices { get; set; }
}
public class Example
{
public IList<OrderItem> orderItems { get; set; }
}
I have tried creating a class:
public class VariationChoice
{
public IList<string> itemIds { get; set; }
public int count { get; set; }
}
and changing the OrderItem class to:
public class OrderItem
{
public string itemId { get; set; }
public IList<Variation> variations { get; set; }
public IList<VariationChoice> variationsChoices { get; set; }
public int price { get; set; }
public int count { get; set; }
public string comment {get; set;}
}
but I get an error when I try to Deserialize the JSON file with
Root Wix;
try
{
Wix = JsonConvert.DeserializeObject<Root>(json);
}
catch (System.Exception ex)
{
string message = ex.Message;
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
The error is:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'WebHook_Receiver.Models.VariationChoice' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'order.orderItems[3].variationsChoices[0]', line 21903, position 6.
Can you help please?
public IList<VariationChoice[]> variationsChoices { get; set; }? and change yourVariationChoiceclass to use a singlestringvalue of theitemId