Javascript json 对象计数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2433664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jSon objects count
提问by coure2011
I have a string streturned 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)
我有一个st从 Web 服务返回的字符串。我将字符串转换为对象,我如何计算没有。里面的数组?(对于这种情况,它是2)
var st = "{[{"Name": "fake", "Address": "add"]},[{"Name": "fake", "Address": "add"}]}";
var json = eval(st);
json.lengthis always returning 1
json.length总是返回1
回答by Bakudan
@coure06 I`ve changed a little bit your JSON. Hope this will be usefull
@coure06 我对你的 JSON 做了一些改动。希望这会很有用
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++;
}
}
回答by T.J. Crowder
I'm surprised json.lengthis returning anything; that JSON string is invalid. The outermost curly braces ({}) denote an object, which must contain keys and values, but it just contains a value (the array, with no key).
我很惊讶json.length返回任何东西;该 JSON 字符串无效。最外面的花括号 ( {}) 表示一个对象,它必须包含键和值,但它只包含一个值(数组,没有键)。
If you remove the curly braces, it should work correctly. Did you put them there, perhaps because you'd seen it done? If so, you want parentheses (()), not curly braces.
如果删除花括号,它应该可以正常工作。你把它们放在那里,也许是因为你看到它完成了吗?如果是这样,您需要括号 ( ()),而不是花括号。
Note that using evalon JSON strings is not safe, you want to use a JSON decoder that doesn't use eval(like json2.js, which uses eval, but only after very carefully ensuring it's safe to do so, modifying it if necessary), for optimum safety. The parentheses help, but they're not at all a complete solution.
请注意,eval在 JSON 字符串上使用是不安全的,您想使用不使用的 JSON 解码器eval(例如json2.js,它使用eval,但只有在非常小心地确保这样做是安全的,必要时修改它之后),对于最佳安全性。括号有帮助,但它们根本不是一个完整的解决方案。
回答by skidadon
Here's the trick!
这就是诀窍!
var jsonString = "{field1:'data1',field2:'data2',arrField1:['arrField1Content1','arrField1Content2']}";
var obj = eval('(' + jsonString + ')');
var objCount=0;
for(_obj in obj) objCount++;
alert(objCount);
回答by Borderless.Nomad
Try using this,
尝试使用这个,
var persons = {
"person1" : {
"Name": "Adam",
"Address": "USA"
},
"person2" : {
"Name": "Jamie",
"Address": "USA"
}
};
Object.prototype.count = function() {
var that = this,
count = 0;
for(property in that) {
if(that.hasOwnProperty(property)) {
count++;
}
}
return count;
};
alert(persons.count());
this could work for nested objects like
这可能适用于嵌套对象,例如
var persons = {
"person1" : {
"Name": "Adam",
"Address": "USA"
},
"person2" : {
"Name": "Jamie",
"Address": "USA" ,
"users": {
"person1" : {
"Name": "Adam",
"Address": "USA"
},
"person2" : {
"Name": "Jamie",
"Address": "USA"
}
}
}
};
alert(persons.person2.users.count());
回答by Pointy
- That is not a valid JSON expression.
- There is no built-in way to count "members" of an object. You can write one, but it's problematic due to the squishy nature of the language.
- 这不是有效的 JSON 表达式。
- 没有内置的方法来计算对象的“成员”。你可以写一个,但由于语言的软弱特性,它是有问题的。
edit— now that I've made it to the future, it's possible to use APIs like Object.keys(), Object.getOwnPropertyNames(), and Object.getOwnPropertySymbolsto get a count of properties in an object:
编辑-现在,我已经去到未来,有可能使用类似的API Object.keys(),Object.getOwnPropertyNames()以及Object.getOwnPropertySymbols获得一个对象的属性的计数:
var props = Object.getOwnPropertySymbols(someObject);
var length = props.length; // number of properties
In my experience, it's pretty rare that I've cared about the number of properties in an object; usually, if you don't know what an object is already, you're interested in specific properties (by name). However there's nothing wrong with using those APIs.
根据我的经验,我很少关心对象中的属性数量。通常,如果您不知道对象是什么,您会对特定属性(按名称)感兴趣。然而,使用这些 API 并没有错。

