如何访问名称中有空格的Json Object? [英] How to access Json Object which has space in its name?

查看:306
本文介绍了如何访问名称中有空格的Json Object?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面查找json回复......

Find below the json response...

{"Object":
          {"PET ANIMALS":[{"id":1,"name":"Dog"},
                          {"id":2,"name":"Cat"}],
           "Wild Animals":[{"id":1,"name":"Tiger"},
                           {"id":2,"name":"Lion"}]
           }
}

在上面提到的回复中,找到PET ANIMALS和野生动物的长度的方法是什么...... ...

In the above mentioned response, what is the way to find the length of "PET ANIMALS" and "Wild ANIMALS"......

推荐答案

var json = /* your JSON object */ ;

json["Object"]["PET ANIMALS"].length // returns the number of array items

使用循环打印数组中的项目数:

Using a loop to print the number of items in the arrays:

var obj = json["Object"];
for (var o in obj) {
    if (obj.hasOwnProperty(o)) {
        alert("'" + o + "' has " + obj[o].length + " items.");
    }
}

这篇关于如何访问名称中有空格的Json Object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆