如何在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作为数组接收并循环遍历数组并获取值。
我该怎么做?
答案 0 :(得分:0)
以下是如何在jQuery中循环对象的元素
$.each(menuitems, function(key, value) {
// Do something with key and value
}