Array index value

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 get the value of the array by index (for example, xxx[0] , as xxx['aaa'] does not like it). How can I do it?

+6
json
source share
7 answers

You can not. Ordering is not guaranteed in json and most other key data structures. In addition, you do not have an array, you have an object. Why use name-value pairs at all if you are not going to use names to access values? This is the power of json and other key-value data stores. If you need to access values โ€‹โ€‹with integer indices, just use an array to store your data.

+13
source share

You can actually use the int index as an array in JSON, just try the following:

 var jsonObject = { "0":{ "status":"available", "classkey":"dotnet" }, "1":{ "ccc":{ "com":"available", "net":"available", "info":"available", "org":"available" } } } alert(jsonObject[0].status) 
+8
source share

You do not have an array, you have an object. Thus, you cannot expect the keys to be in the same order on all systems. Iterate over the object, since the objects are expected to repeat, that is, over the keys that you specified.

+3
source share

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

 for (var key in xxx) { document.write(xxx[key]); } 
+1
source share

JSON has never been used, but according to http://labs.adobe.com/technologies/spry/samples/data_region/JSONDataSetSample.html#Example1 you can create an ordered array. Just remove the key and colon in your key-value pairs. (Someone please tell me if this works - it may depend on the values โ€‹โ€‹defined in all objects, or it may contain empty columns).

0
source share

Perhaps this may solve your problem.

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

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

To provide an answer to the title of the question, here is an array structure that will serve the original purpose:

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

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

0
source share

All Articles