开发者

Access element in JSON having numerical index

开发者 https://www.devze.com 2022-12-28 20:30 出处:网络
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.

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": [
          开发者_JS百科  "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"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号