2

I've parsed the json string and I am trying to access the elements. I am unable to access link1, link2, link3

{
"click_title":"GO",

"links":

    {

    "commonlink":"http:\/\/bookings.com",

    "alllinks":
        [
        [
            {

                "link1":"http:\/\/xyz1.com\/get\/a",

                "link2":"http:\/\/www.anotherwebsite1.com\/c\/t",

                "link3":"http:\/\/www.newsite1.com\/v\/h"
            },

            {

                "link1":"http:\/\/xyz2.com\/get\/a",

                "link2":"http:\/\/www.anotherwebsite2.com\/c\/t",

                "link3":"http:\/\/www.newsite2.com\/v\/h"

            }
        ],

        [
            {

                "link1":"http:\/\/xyz3.com\/get\/a",

                "link2":"http:\/\/www.anotherwebsite3.com\/c\/t",

                "link3":"http:\/\/www.newsite3.com\/v\/h"
            }
        ]
        ]
    }
}

var data = $.parseJSON(dbData);

I am able to access click_title and commonlink using this :-

data.click_title
data.links['commonlink']

but not able to access link1, link2, link3. I tried

data.links['alllinks'] which returns [object Object]. 

If I try

data.links['alllinks']['link1'] i get undefined
Stacy J
  • 2,721
  • 15
  • 58
  • 92
  • If you stop using alerts for debugging, and start using the console, you'll actually see the objects structure instead of `[object, object]`. – adeneo Mar 30 '13 at 14:43

1 Answers1

1

The structure is very odd. alllinks is an array of arrays of objects (note the [ and second [ after "alllinks":, each of those starts an array). To get at the first link1 in the first array, you'd do:

var link = data.links.alllinks[0][0].link1;

It's easier to see that if you format your JSON consistently. Here I've used http://jsonlint.com to clean it up, and then I've added some comments (not that comments are valid in JSON):

{
    "click_title": "GO",
    "links": {
        "commonlink": "http://bookings.com",
        "alllinks": [ // <== Starts the outer array
            [         // <== Starts an inner array
                {     // <== Starts the object that's the first entry
                    "link1": "http://xyz1.com/get/a",
                    "link2": "http://www.anotherwebsite1.com/c/t",
                    "link3": "http://www.newsite1.com/v/h"
                },
                {     // <== Starts the object that's the second entry
                    "link1": "http://xyz2.com/get/a",
                    "link2": "http://www.anotherwebsite2.com/c/t",
                    "link3": "http://www.newsite2.com/v/h"
                }
            ],        // <== Ends the first inner array
            [         // <== Starts the second inner array
                {
                    "link1": "http://xyz3.com/get/a",
                    "link2": "http://www.anotherwebsite3.com/c/t",
                    "link3": "http://www.newsite3.com/v/h"
                }
            ]         // <== Ends the second inner array
        ]             // <== Ends the outer array
    }
}

So in total, you have three link1's: The one on the object at [0][0], the one on the object at [0][1], and the one on the object at [1][0].

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875