最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论

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 :(

发布评论

评论列表(0)

  1. 暂无评论