JSON枚举列表反序列化

时间:2016-04-14 10:32:46

标签: c# json json.net

我有JSON,如下所示

{
    "_id": "0FEB6D4B-8DA5-4143-B926-11A7AE4F3B12",
    "device": {
        "name": "test",
        "family": "test"
    },
    "channels": [
        {
            "mcl": 33,
            "vtype": "FLOAT",
            "category": " Current"
        },
        {
            "tag": "OperationMode",
            "vtype": "BYTE",
            "enums": [
                {
                    "0": "Off"
                },
                {
                    "1": "On"
                },
                {
                    "2": "ByPass"
                }
            ]
        }
    ]
}

我正在使用Json.net将JSON反序列化为C#对象。我无法设法转换枚举列表。我验证了这是否是有效的JSON。我试过定制转换器&字符串转换器,但读者值为null。任何快速帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

JObject jR = JObject.Parse(YourJsonString);
JArray oR = (JArray)jR["channels"];     

JArray jA = (JArray)oR[1]["enums"];
foreach (var item in jA)
{
    Dictionary<string,string> enums = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string,string>>(item.ToString());            
    foreach (var en in enums)
    {
        Console.WriteLine(en.Value);
    }
}

Fiddler:https://dotnetfiddle.net/ziSep1