简体   繁体   中英

How to deserialize this json to a list of objects?

I got this Json msg (don't pay attention to the encoding) :

{
"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. What structure should i use to deserialize this to a List of ProductTypeDTO? Thx

As Anton noted in his comment, you should deserialize to a Dictionary<string, ProductTypeDTO> first.

Code (uses JSON.NET):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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