如何在Javascript中解析以下JSON?

时间:2013-08-24 09:15:51

标签: javascript json parsing

我正在为诺基亚S40平台制作一个网络应用程序。我使用返回以下JSON的Javascript调用Web服务

{ "status_code": 200, "status_txt": "OK", "data": { "expand": [ { "short_url": "http:\/\/bit.ly\/msapps", "long_url": "http:\/\/www.microsoft.com\/web\/gallery\/?wt.mc_id=soc-in-wag-msp-M389", "user_hash": "gbL9jV", "global_hash": "eHgpGh" } ] } } 

我想获得“short_url”和“long_url”值

我使用 eval 作为var obj = eval ("(" + xmlhttp.responseText + ")");

其中 xmlhttp.responseText 包含JSON响应。 请帮忙

3 个答案:

答案 0 :(得分:9)

试过这个并且工作

 var s = '{ "status_code": 200, "status_txt": "OK", "data": { "expand": [ { "short_url": "http://bit.ly/msapps", "long_url": "http://www.microsoft.com/web/gallery/?wt.mc_id=soc-in-wag-msp-M389", "user_hash": "gbL9jV", "global_hash": "eHgpGh" } ] } } '

 var d = JSON.parse(s);
 console.log(d.data.expand[0].short_url);
 console.log(d.data.expand[0].long_url);

答案 1 :(得分:1)

此表达式

JSON.parse('{ "status_code": 200, "status_txt": "OK", "data": { "expand": [ { "short_url": "http:\/\/bit.ly\/msapps", "long_url": "http:\/\/www.microsoft.com\/web\/gallery\/?wt.mc_id=soc-in-wag-msp-M389", "user_hash": "gbL9jV", "global_hash": "eHgpGh" } ] } }').data.expand[0].short_url

返回“http://bit.ly/msapps

答案 2 :(得分:-3)

这个怎么样

var json = "{}" // Your JSON string
json = new Function('return ' + json)();
console.log(json.data.expand[0].short_url, json.data.expand[0].long_url);
相关问题