用动态密钥JSON反序列化

用动态密钥JSON反序列化

问题描述:

我是很新的JSON和我目前了解的(de)序列化。
我从检索网页JSON字符串,并试图将其反序列化为一个对象。问题是,根JSON关键是静态的,但底层键是动态的,我不能预料他们反序列化。下面是字符串的一个小例子:

I'm quite new to JSON, and am currently learning about (de)serialization. I'm retrieving a JSON string from a webpage and trying to deserialize it into an object. Problem is, the root json key is static, but the underlying keys are dynamic and I cannot anticipate them to deserialize. Here is a mini example of the string :

{"daily":{"1337990400000":443447,"1338076800000":444693,"1338163200000":452282,"1338249600000":462189,"1338336000000":466626}

有关另一在我的应用JSON字符串,我使用的是的JavaScriptSerializer和预测使用类结构的钥匙。 ?什么是去反序列化这个字符串转换为对象的最佳方式。

For another JSON string in my application, I was using a JavascriptSerializer and anticipating the keys using class structure. What's the best way to go about deserializing this string into an object?

说真的,没有必要去下动态路由;用

Seriously, no need to go down the dynamic route; use

var deser = new JavaScriptSerializer()
    .Deserialize<Dictionary<string, Dictionary<string, int>>>(val);
var justDaily = deser["daily"];



得到一个字典,然后你就可以如

to get a dictionary, and then you can e.g.

foreach (string key in justDaily.Keys)
    Console.WriteLine(key + ": " + justDaily[key]);

要拿到钥匙现在和相应的值。

to get the keys present and the corresponding values.