53

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.

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.

Please anyone tell me how to find the length.... Thanks....

coderpc
  • 2,685
  • 3
  • 31
  • 58

4 Answers4

94

Before going to answer read this Documentation once. Then you clearly understand the answer.

Try this It may work for you.

Object.keys(data.shareInfo[i]).length
James
  • 2,558
  • 2
  • 25
  • 52
15

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;
ahjmorton
  • 885
  • 1
  • 5
  • 18
  • It produces the error `shareInfo` is not defined. But I have the array called `shareInfo` –  Mar 04 '13 at 19:23
  • @ahjmorton your answer is correct for `javascript` arrays. If it is json array then it must be `Object.keys(data.shareInfo[i]).length` – James Mar 04 '13 at 19:39
0

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);
}
Kabir
  • 2,026
  • 4
  • 18
  • 24
0

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.

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.)

Here is a good JSON reference to clarify this difference:

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.html for a reference.

Scott Leonard
  • 736
  • 7
  • 19
  • You are right. But we can pass the values from server by using JSONarray. For archive this we need to create `JSONarray` ->>then `toJsonTree` Then we can convert the `object` to `String`. In client side we will receive result as above .( as array in the question). – James Mar 04 '13 at 19:48