Modern browsers support JSON.parse().
var arr_from_json = JSON.parse( json_string );
Pergunta
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?
Solução
Modern browsers support JSON.parse().
var arr_from_json = JSON.parse( json_string );
Outras dicas
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