【发布时间】:2021-05-24 04:51:02
【问题描述】:
我正在使用https://jsonutils.com/ 从 JSON 构建 C# 类,但我被困在变体选择的正确类上。我遇到问题的 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
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; }
}
我尝试过创建一个类:
public class VariationChoice
{
public IList<string> itemIds { get; set; }
public int count { 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;}
}
但是当我尝试使用反序列化 JSON 文件时出现错误
Root Wix;
try
{
Wix = JsonConvert.DeserializeObject<Root>(json);
}
catch (System.Exception ex)
{
string message = ex.Message;
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
错误是:
无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“WebHook_Receiver.Models.VariationChoice”,因为该类型需要 JSON 对象(例如 {"name":"value"})才能正确反序列化. 要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),例如可以从 JSON 数组反序列化。 JsonArrayAttribute 也可以添加到类型中以强制它从 JSON 数组反序列化。 路径 'order.orderItems[3].variationsChoices[0]',第 21903 行,位置 6。
你能帮忙吗?
【问题讨论】:
-
看起来像一个数组,你试过
public IList<VariationChoice[]> variationsChoices { get; set; }吗?并更改您的VariationChoice类以使用itemId的单个string值