在jquery中解析嵌套的JSON

时间:2013-11-26 21:46:14

标签: jquery json

如何在Javascript中解析嵌套的Json对象?

构成Json字符串并返回客户端的PHP程序

$menu_items = array(
  "Account" => "account",
  "Send SMS" => "send-sms",
  "Remote Address Book" => "remote-address-book"
);

$arrLoginResponse = array (
  "success" => true,
  "url" => "someurl",
  "token" => "100",
  "menuitems" => $menu_items
);
$jsnLoginResponse = json_encode($arrLoginResponse);

return $jsnLoginResponse;

在jQuery中我有

var jsnObj = jQuery.parseJSON(loginResponse);
var username = jsnObj.url;  //this works fine and return the url string
var menuitems = jsnObj.menuitems; //this is returning as an object

我希望将menuitems作为数组接收并循环遍历数组并获取值。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

以下是如何在jQuery中循环对象的元素

$.each(menuitems, function(key, value) {
    // Do something with key and value
}