开发者

reading JSON data in javascript

I have this JSON object

{"stores":"{
               "1":{
                     "name":"Publix",
                   "add开发者_运维知识库ress":"1fbdfhbdhsdhsrh",
                   "long":"-84.012502",
                   "lat":"33.878952"},
               "2":{
                     "name":"Publix",
                    "address":"fgsregerge",
                     "long":"-84.125147",
                    "lat":"33.9448"}
          }"

}

this is returned in a jquery.ajax call as datatype:json. I am able to access data.stores and that displays all the stores in alert box but data.stores.1 does not work.. How can I read this properly?


You could use data.stores["1"], but really you seem to be representing an array in a really odd way.

You should just use an array instead:

{
    "stores": [
        {
            "name": "Publix",
            "address": "1fbdfhbdhsdhsrh",
            "long": "-84.012502",
            "lat": "33.878952"
        },
        {
            "name": "Publix",
            "address": "fgsregerge",
            "long": "-84.125147",
            "lat": "33.9448"
        }
    ]
}

Then, you can access it as such:

data.stores[0] and data.stores[1].


Remove the unnecessary quotes after "stores": The JSON should now look like:

var dat = {
    "stores": {
        "1": {
            "name": "Publix",
            "address": "1fbdfhbdhsdhsrh",
            "long": "-84.012502",
            "lat": "33.878952"
        },
        "2": {
            "name": "Publix",
            "address": "fgsregerge",
            "long": "-84.125147",
            "lat": "33.9448"
        }
    }
};

and also try using this code:

alert(dat.stores["1"]);

Working example: http://jsfiddle.net/mstjA/


That JSON isn't valid. Might just be a typo from when you brought it into Stack.

var data = {
    "stores": {
        "1": {
            "name": "Publix",
            "address": "1fbdfhbdhsdhsrh",
            "long": "-84.012502",
            "lat": "33.878952"
        },
        "2": {
            "name": "Publix",
            "address": "fgsregerge",
            "long": "-84.125147",
            "lat": "33.9448"
        }
    }
}

You can't use the dot notation to reference a key that is numeric. You'll need to use brackets. These two are equivalent:

var a = data.stores["1"].name; /* a = "Publix" */
var b = data["stores"]["1"]["name"]; /* b = "Publix" */

Happy coding!


There is something weird about this JSON object: it is not valid. You have double quotes around the stores value. I guess they should be removed so that the stores property is not a JSON string but a JSON object. Then you could access it like this:

alert(data.stores['1'].name);

Here's a live demo.

If the double quotes are indeed present, well, you have invalid javascript because you are not properly escaping the double quotes inside.


data.stores['1'] will give you the json object and then you can access the properties within it like

var stores = data.stores['1'];
var name = stores.name;
var address = stores.address;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜