简体   繁体   中英

Access element in JSON having numerical index

I have following format of JSON in which I want to access 0.4, kem, 2, 2000 values but it seems it doesn't have name index so how one can access it in jQuery.

When I paste following code in JSON viewer then I am getting numerical index for 0.4, kem, 2

"td": [
    {
        "@attributes": {
            "class": "odd" 
        },
        "span": [
            "3",
            "7" 
        ] 
    },
    "0.4",
    "Kem",
    "24\/04\/2010",
    "2000",
    "2",
    "14000",
    "Good",
    "Buckley",
    "56.0",
    "2:05.32",
    "36.65",
    "54.5" 
]
}

First of all, your parentheses don't match up; I'm assuming you meant to have an open curly brace at the very beginning of your code sample.

If so, then this is simply an object containing one field, "td". That field is an array. The array contains a number of items, the first of which is an object and the rest are strings.

So, if you want to access one of the strings, you would have to use the numeric index or else iterate over the array. For example:

var myJSON =
{"td": [
    {
        "@attributes": {
            "class": "odd"
        },
        "span": [
            "3",
            "7"
        ]
    },
    "0.4",
    "Kem",
    "24\/04\/2010",
    "2000",
    "2",
    "14000",
    "Good",
    "Buckley",
    "56.0",
    "2:05.32",
    "36.65",
    "54.5"
]
};

alert (myJSON.td[4]); //displays "2000"
alert (myJSON.td[7]); //displays "Good"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM