9

I have a json file with contents like this:

{
     "aaa":{
         "status":"available",
         "classkey":"dotnet"
     },
     "bbb":{
         "ccc":{
             "com":"available",
             "net":"available",
             "info":"available",
             "org":"available"
          }
     }
}

Now I want to fetch the value of array by index (ex., xxx[0] like this not like xxx['aaa']). How do I do that?

1
  • 2
    Maybe if you explained why you want to do it this way, we could help you find a better solution. Commented Nov 20, 2010 at 5:12

7 Answers 7

15

You can't. Order is not guaranteed in json and most other key-value data structures. Furthermore you don't have an array, you have an object. Why use name-value pairs at all if you aren't going to use the names to access the values? Therein lies the power of json and other key-value data stores. If you need to access the values by integer indexes, then just use an array to store your data.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 But ... does JSON itself explicitly prohibit ordering (it does not guarantee it, but it is preserved in the wire-format, if one were to be silly to rely on such a deterministic generation)? I'd imagine that it's the implementation that don't guarantee it... anyway... just a silly ramble.
9

Actually you can use int index like an array in JSON, just try that:

var jsonObject = {
     "0":{
         "status":"available",
         "classkey":"dotnet"
     },
     "1":{
         "ccc":{
             "com":"available",
             "net":"available",
             "info":"available",
             "org":"available"
          }
     }
}

alert(jsonObject[0].status)

1 Comment

@Xdevelop Could you please provide a PHP script with while loop to create an array as this?
3

You don't have an array, you have an object. As such you cannot expect the keys to be in the same order on all systems. Iterate the object as objects are expected to be iterated, that is, over the keys you are given.

Comments

1

For the sake of providing an answer to the question title, here is the array structure that would serve the original purpose:

[
     {
         "status":"available",
         "classkey":"dotnet"
     },
     {
         "ccc":{
             "com":"available",
             "net":"available",
             "info":"available",
             "org":"available"
          }
     }
]

So just replace the outmost brackets with [] to get an array instead of an object. Here is a demo. See http://www.json.org/ and http://www.json.com/.

Comments

1

Use the for...in construct, and then use array syntax. Here is an example.

for (var key in xxx) {
    document.write(xxx[key]);
}

1 Comment

i still refers to the key name, even if you name it i. :)
0

Never used JSON, but according to http://labs.adobe.com/technologies/spry/samples/data_region/JSONDataSetSample.html#Example1 , it's possible to create an ordered array. Just remove the key and colon in your key-value pairs. (Someone please tell me whether this works - it may be dependent on values being defined in all objects, or it may allow null columns).

Comments

0

Maybe this can solve your problem.

Using jQuery you can convert your JSon to Array and access it by index.

var data = $.parseJSON(msg.d ? msg.d : msg);
alert(data[1].status)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.