i am converting my ajax code from xml to json, but i am missing something basic here:
when i receive the json-string on the client-side, what is the remended way to convert it into a javascript object.
for example i get this string:
{"connectionid":12345}
and i would like to do something like this:
alert(xmlhttp.responseText.connectionid);
thanks!
i am converting my ajax code from xml to json, but i am missing something basic here:
when i receive the json-string on the client-side, what is the remended way to convert it into a javascript object.
for example i get this string:
{"connectionid":12345}
and i would like to do something like this:
alert(xmlhttp.responseText.connectionid);
thanks!
Share Improve this question asked Feb 7, 2011 at 12:34 clampclamp 34.1k75 gold badges208 silver badges307 bronze badges 1- you could use the evil eval :) – Shrinath Commented Feb 7, 2011 at 12:37
4 Answers
Reset to default 8Use JSON.parse()
, or eval()
, if you like to live dangerously (or fully trust where your JSON es from).
If you happen to be using jQuery, you get $.parseJSON()
.
Most browsers (the recent ones at least.. not IE7) have a native JSON object that you can use to parse and stringify JSON.
alert(JSON.parse(xmlhttp.responseText).connectionid);
In browsers that don't support the JSON object, you can either use a JSON parser from JSON or use eval(), however eval() is quite dangerous and i definitly don't advise you to use it.
Call eval on the response text.
var response = eval(xmlHttp.responseText);
alert(response.connectionId);
you could use eval
check this out : http://www.json/js.html
edit - oops, others typed faster :(
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744397832a4572234.html
评论列表(0条)