jSon对象计数 [英] jSon objects count

查看:288
本文介绍了jSon对象计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从Web服务返回的字符串 st 。我将字符串转换为对象,我如何计算否。里面的数组? (对于这种情况,它是 2

I have a string st returned from a web service. I convert the string to an object, how can I count the no. of arrays inside it? (for this case it's 2)

var st = "{[{"Name": "fake", "Address": "add"]},[{"Name": "fake", "Address": "add"}]}";  
var json = eval(st);  

json.length 总是返回 1

推荐答案

@ coure06我改变了一点你的JSON。希望这将是有用的

@coure06 I`ve changed a little bit your JSON. Hope this will be usefull

var persons = {
    "person1" : {
        "Name": "Adam",
        "Address": "USA" 
    },
    "person2" : {
        "Name": "Jamie",
        "Address": "USA" 
    }
};
var count = 0;
//var count = persons.length;          // wont work
for ( property in persons )          // should return 2
{
   if(persons.hasOwnProperty(property))
   {
      count++;
   }
}

jsfiddle的实例

这篇关于jSon对象计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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