I have a dictionary that is serialised with JavaScriptSerializer from C#.

On the client side I have:

"{"dd049eda-e289-4ca2-8841-4908f94d5b65":"2","ab969ac2-320e-42e1-b759-038eb7f57178":"5"}"

How can I deserialise it so I can have a key-value pair array?

有帮助吗?

解决方案

Modern browsers support JSON.parse().

var arr_from_json = JSON.parse( json_string );

其他提示

That will parse into an object with GUID keys.

To enumerate them, you can use:

var jsonString = '{"dd049eda-e289-4ca2-8841-4908f94d5b65":"2","ab969ac2-320e-42e1-b759-038eb7f57178":"5"}';
var map = JSON.parse(jsonString);
var keys = Object.keys(map);
for (var i =0; i < keys.length; i++)
{
    var key = keys[i];
    console.log(key, '=', map[key]);
}

This will output:

dd049eda-e289-4ca2-8841-4908f94d5b65 = 2
ab969ac2-320e-42e1-b759-038eb7f57178 = 5 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top