views:

70

answers:

1

i have following format of json in which i want to asscess 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" 
]
}
+3  A: 

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"
JacobM
Thanks it worked !!
hunt
+1 for figuring out what he was asking.
Robusto