简体   繁体   English

如何将此json反序列化为对象列表?

[英]How to deserialize this json to a list of objects?

I got this Json msg (don't pay attention to the encoding) : 我得到了这个Json消息(不注意编码):

{
"U1000 R2V": {
    "carac1": ["Câble d'alimentation rigide rond"],
    "optionVJ": ["Oui"],
    "carac4": [""],
    "carac3": ["Pour alimentation de ligne électrique", "Pour alimentation des moteurs"]
},
"H05 VVF": {
    "carac1": ["Câble souple rond"],
    "optionVJ": ["Oui"],
    "carac4": [""],
    "carac3": ["Pour câblage des sélecteurs à clé", "Pour alimentation des lampe clignotantes"]
}
}

I've tried with those objects : 我已经尝试过使用这些对象:

public class TypeProductList
{
    public List<ProductTypeDTO> cables { get; set; }
}


public class ProductTypeDTO
{
    public string[] carac1;
    public string[] optionVJ;
    public string[] carac4;
    public string[] carac3;
}

But this doesn't work, actually i don't know how to do because the "parent element" (i mean U1000 R2V, H05 VVF) is changing for every object. 但这是行不通的,实际上我不知道该怎么做,因为每个对象的“父元素”(我的意思是U1000 R2V,H05 VVF)都在变化。 What structure should i use to deserialize this to a List of ProductTypeDTO? 我应使用什么结构将其反序列化为ProductTypeDTO列表? Thx 谢谢

As Anton noted in his comment, you should deserialize to a Dictionary<string, ProductTypeDTO> first. 正如Anton在其评论中指出的那样,您应该首先反序列化为Dictionary<string, ProductTypeDTO>

Code (uses JSON.NET): 代码(使用JSON.NET):

var result = JsonConvert.DeserializeObject<Dictionary<string, ProductTypeDTO>>(json);
var typeProductList = new TypeProductList { cables = result.Values.ToList(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM