2

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?

3
  • maybe try data.stores[1] Commented Aug 9, 2011 at 14:44
  • There are some extra double quotes around the stores. 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 Commented Aug 9, 2011 at 14:46

5 Answers 5

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].

Sign up to request clarification or add additional context in comments.

Comments

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/

Comments

1

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!

Comments

0

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.

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.