Index value of Array

前端 未结 7 1602
眼角桃花
眼角桃花 2021-01-12 08:56

I have a json file with contents like this:

{
     \"aaa\":{
         \"status\":\"available\",
         \"classkey\":\"dotnet\"
     },
     \"bbb\":{
              


        
相关标签:
7条回答
  • 2021-01-12 09:14

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

    for (var key in xxx) {
        document.write(xxx[key]);
    }
    
    0 讨论(0)
  • 2021-01-12 09:15

    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.

    0 讨论(0)
  • 2021-01-12 09:24

    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)
    
    0 讨论(0)
  • 2021-01-12 09:24

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

    0 讨论(0)
  • 2021-01-12 09:26

    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.

    0 讨论(0)
  • 2021-01-12 09:26

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

    0 讨论(0)
提交回复
热议问题