xml - json: how to get the json-string into a javascript object - Stack Overflow

i am converting my ajax code from xml to json, but i am missing something basic here:when i receive the

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
Add a ment  | 

4 Answers 4

Reset to default 8

Use 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信