最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - reading JSON data in javascript - Stack Overflow

programmeradmin0浏览0评论

I have this JSON object

{"stores":"{
               "1":{
                     "name":"Publix",
                   "address":"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?

I have this JSON object

{"stores":"{
               "1":{
                     "name":"Publix",
                   "address":"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?

Share Improve this question asked Aug 9, 2011 at 14:42 locconfusedlocconfused 591 silver badge5 bronze badges 3
  • maybe try data.stores[1] – Sleeperson Commented Aug 9, 2011 at 14:44
  • There are some extra double quotes around the stores. – Reto Aebersold Commented Aug 9, 2011 at 14:45
  • yes, you can write object.attribute as object['attribute'], if there's some "problem" with the attribute name – mykhal Commented Aug 9, 2011 at 14:46
Add a ment  | 

5 Answers 5

Reset to default 3

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

  1. 暂无评论