从JSON构建C#类

从JSON构建C#类,c#,json,class,C#,Json,Class,我正在使用JSON构建C#类,但我一直在寻找variationsChoices的正确类。JSON文件中我遇到问题的部分是: {"orderItems": [ { "itemId": "92953385-4ce1-44be-8a20-c3cc597dd4a7", "price": 325, "count": 1

我正在使用JSON构建C#类,但我一直在寻找variationsChoices的正确类。JSON文件中我遇到问题的部分是:

{"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返回以下类,但public IList variationsChoices{get;set;}不是值C#

公共类标题
{
公共字符串en_GB{get;set;}
}
公共阶级变异
{
公共标题标题{get;set;}
公共IList ItemId{get;set;}
public int minNumAllowed{get;set;}
public int maxNumAllowed{get;set;}
公共字符串显示类型{get;set;}
}
公共类OrderItem
{
公共字符串itemId{get;set;}
公共整数价格{get;set;}
公共整数计数{get;set;}
公共IList变体{get;set;}
公共IList变量schoices{get;set;}
}
公开课范例
{
公共IList orderItems{get;set;}
}
我已尝试创建一个类:

public class VariationChoice
{
    public IList<string> itemIds { get; set; }
    public int count { get; set; }
}
公共类变量选择
{
公共IList ItemId{get;set;}
公共整数计数{get;set;}
}
并将OrderItem类更改为:

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;}
}
公共类OrderItem
{
公共字符串itemId{get;set;}
公共IList变体{get;set;}
公共IList变量schoices{get;set;}
公共整数价格{get;set;}
公共整数计数{get;set;}
公共字符串注释{get;set;}
}
但是,当我尝试使用反序列化JSON文件时,出现了一个错误

Root Wix;
        try
        {
            Wix = JsonConvert.DeserializeObject<Root>(json);
        }
        catch (System.Exception ex)
        {
            string message = ex.Message;
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
根Wix;
尝试
{
Wix=JsonConvert.DeserializeObject(json);
}
catch(System.Exception-ex)
{
字符串消息=例如消息;
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
错误是:

无法将当前JSON数组(例如[1,2,3])反序列化为类型“WebHook_Receiver.Models.VariationChoice”,因为该类型需要JSON对象(例如{“name”:“value”})才能正确反序列化。 要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”}),或将反序列化类型更改为数组或实现可从JSON数组反序列化的集合接口(例如ICollection、IList)类似列表的类型。还可以将JsonArrayAttribute添加到类型中,以强制它从JSON数组反序列化。 路径“order.orderItems[3].variationsChoices[0]”,第21903行,位置6


您能帮忙吗?

这里您正在创建一个包含字符串数组和int类型的类

 public class VariationChoice
 {
    public IList<string> itemIds { get; set; }
    public int count { get; set; }
 }
序列化程序不知道如何将json类型“转换”为c类型,因为它们不兼容

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

  public class VariationChoice
  {
      public string itemId { get; set; }
      public int count { get; set; }
  }
公共类变量选择
{
公共IList变量选择{get;set;}
}
公共类变量选择
{
公共字符串itemId{get;set;}
公共整数计数{get;set;}
}

这应该可以用

在这里,您正在创建一个包含字符串数组和int类型的类

 public class VariationChoice
 {
    public IList<string> itemIds { get; set; }
    public int count { get; set; }
 }
序列化程序不知道如何将json类型“转换”为c类型,因为它们不兼容

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

  public class VariationChoice
  {
      public string itemId { get; set; }
      public int count { get; set; }
  }
公共类变量选择
{
公共IList变量选择{get;set;}
}
公共类变量选择
{
公共字符串itemId{get;set;}
公共整数计数{get;set;}
}

多亏了@Ryan Wilson和@CaveCoder,这应该能奏效。你们都正确地指出VariationChoices是数组的数组。对我有效的解决方案是:

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

我把你的两个答案结合起来得到了这个答案。再次非常感谢

非常感谢@Ryan Wilson和@CaveCoder。你们都正确地指出VariationChoices是数组的数组。对我有效的解决方案是:

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

我把你的两个答案结合起来得到了这个答案。再次感谢

看起来像一个数组数组,您是否尝试过
公共IList variationsChoices{get;set;}
?并将
VariationChoice
类更改为使用单个
string
值,该
itemId
看起来像一个数组数组,您是否尝试过
public IList variationsChoices{get;set;}
?并将
VariationChoice
类更改为使用
itemId