0

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?

1
  • 1
    looks like an array of arrays, have you tried public IList<VariationChoice[]> variationsChoices { get; set; }? and change your VariationChoice class to use a single string value of the itemId Commented Feb 21, 2021 at 21:41

2 Answers 2

1

Here you are creating a class containing an array of strings and an int type.

 public class VariationChoice
 {
    public IList<string> itemIds { get; set; }
    public int count { get; set; }
 }

but here you are declaring a member of type array of array, with the inner array containing one string member and one numeric member

"variationsChoices": [
                [
                    {
                        "itemId": "86bef61f-a59f-4c1f-88a6-009594ed1f54",
                        "count": 1
                    }
                ]
            ],

The serializer does not konw how to "translate" from json type to c# type because they are not compatible

public class VariationChoices
  {
      public IList<VariationChoices> variationChoices { get; set; }
  }

  public class VariationChoice
  {
      public string itemId { get; set; }
      public int count { get; set; }
  }

this should work

Sign up to request clarification or add additional context in comments.

Comments

1

Many thanks to @Ryan Wilson and @CaveCoder. You are both correct in pointing out that VariationChoices is an array of arrays. The solution that worked for me was:

 public class OrderItem
{
    public string itemId { get; set; }
    public IList<Variation> variations { get; set; }
    public IList<IList<VariationChoice>> variationsChoices { get; set; }
    public int price { get; set; }
    public int count { get; set; }
    public string comment {get; set;}
}

and create a class

public class VariationChoice
{
    public string itemId { get; set; }
    public int count { get; set; }
}

I got to this answer by combining both your answers. Many thanks again

Comments

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.