从json到字典的反序列化对象

时间:2016-05-31 13:32:54

标签: c# json

我已经从JSON反序列化了一个对象:

dynamic bookdata = JsonConvert.DeserializeObject(bookdata_);
dynamic boy = orderbookdata.asks;

现在,动态boy包含以下数据:

{[ [25,32], [33,44], [22,11], [57.98,2.22] ]}

我想将这些数据放入更有条理的格式,而不是将其保存在boy中。例如。字典等...

json结构是这样的:

{
    "name": "aal",
    "symbol": "ltd",
    "timestamp": 1464703002,
    "asks": [
        [25, 32],
        [33, 44],
        [22, 11],
        [57.98, 2.22]
    ]
}

我想将[25, 32], [33, 44], [22, 11], [57.98, 2.22]提取到每个单独的值中,然后弹出两列列表视图。

1 个答案:

答案 0 :(得分:1)

如果dynamic boy包含以下数据{[ [ 25, 32 ], [ 33, 44 ], [ 22, 11 ], [ 57.98, 2.22 ]]},请参阅以下代码:

dynamic bookdata = JsonConvert.DeserializeObject(bookdata_);
dynamic boy = orderbookdata.asks;
double[][] e = ((IEnumerable<dynamic>)boy)
                 .Select(b => ((IEnumerable<dynamic>)b)
                                .Cast<double>()
                                .ToArray())
                 .ToArray();

如果需要Dictionary<first_value, second_value>

dynamic bookdata = JsonConvert.DeserializeObject(bookdata_);
dynamic boy = orderbookdata.asks;
Dictionary<double, double> d = ((IEnumerable<dynamic>)boy)
                                 .Select(b => ((IEnumerable<dynamic>)b)
                                                .Cast<double>())
                                 .ToDictionary(i => i.First(), i => i.Last());