jQuery 如何计算JSON数组元素的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15209136/
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
how to count length of the JSON array element
提问by James
I'm trying to count the length of the JSON array element. I know to count the length of array using json.array.length
. That is need to find that how many items in every index.
我正在尝试计算 JSON 数组元素的长度。我知道使用json.array.length
. 那就是需要找出每个索引中有多少项。
If my array is:
如果我的数组是:
{"shareInfo":[{"id":"1","a":"sss","b":"sss","question":"whi?"},
{"id":"2","a":"sss","b":"sss","question":"whi?"},
{"id":"3","a":"sss","b":"sss","question":"whi?"},
{"id":"4","a":"sss","b":"sss","question":"whi?"}]}
Then I need to find the length of {"id":"1","a":"sss","b":"sss","question":"whi?"}
. In this there have four items. I tried this with data.shareInfo[i].length
. But it produces error.
然后我需要找到{"id":"1","a":"sss","b":"sss","question":"whi?"}
. 在这有四个项目。我用data.shareInfo[i].length
. 但它会产生错误。
Please anyone tell me how to find the length.... Thanks....
请任何人告诉我如何找到长度....谢谢....
回答by James
Before going to answer read this Documentationonce. Then you clearly understand the answer.
在回答之前,请阅读本文档一次。那么你就清楚地明白了答案。
Try this It may work for you.
试试这个它可能对你有用。
Object.keys(data.shareInfo[i]).length
回答by ahjmorton
First if the object you're dealing with is a string then you need to parse it then figure out the length of the keys :
首先,如果您正在处理的对象是一个字符串,那么您需要解析它然后找出键的长度:
obj = JSON.parse(jsonString);
shareInfoLen = Object.keys(obj.shareInfo[0]).length;
回答by Scott Leonard
First, there is no such thing as a JSON object. JSON is a string format that can be used as a representation of a Javascript object literal.
首先,没有 JSON 对象这样的东西。JSON 是一种字符串格式,可用作 Javascript 对象文字的表示。
Since JSON is a string, Javascript will treat it like a string, and not like an object (or array or whatever you are trying to use it as.)
由于 JSON 是一个字符串,Javascript 会将它视为字符串,而不是像对象(或数组或任何您试图将其用作的东西)。
Here is a good JSON reference to clarify this difference:
这是一个很好的 JSON 参考来阐明这种差异:
http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/
http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/
So if you need accomplish the task mentioned in your question, you must convert the JSON string to an object or deal with it as a string, and not as a JSON array. There are several libraries to accomplish this. Look at http://www.json.org/js.htmlfor a reference.
因此,如果您需要完成问题中提到的任务,则必须将 JSON 字符串转换为对象或将其作为字符串处理,而不是作为 JSON 数组处理。有几个库可以实现这一点。查看http://www.json.org/js.html以获取参考。
回答by Kabir
I think you should try
我觉得你应该试试
data = {"shareInfo":[{"id":"1","a":"sss","b":"sss","question":"whi?"},
{"id":"2","a":"sss","b":"sss","question":"whi?"},
{"id":"3","a":"sss","b":"sss","question":"whi?"},
{"id":"4","a":"sss","b":"sss","question":"whi?"}]};
ShareInfoLength = data.shareInfo.length;
alert(ShareInfoLength);
for(var i=0; i<ShareInfoLength; i++)
{
alert(Object.keys(data.shareInfo[i]).length);
}