-1

So i'm running a $.getJSON statement and i'm having some problems... here's the json:

{
    "K-6608-1-0": [
        {
            "Info": [
                {
                    "SVGFile": "46658.svg",
                    "Name": "Faucet Parts"
                }
            ],
            "Parts": [
                {
                    "Cod":"70012",
                    "Name":"Ruela de Parafuso Reforçado B2",
                    "Price":"$100"
                },
                {
                    "Cod":"71131",
                    "Name":"Parafusasdasdasdsdao Reforçado B2",
                    "Price":"$45"
                },
                {
                    "Cod":"78208",
                    "Name":"Tubo de Conexão R2D2",
                    "Price":"$150"
                }
            ]
        }
    ]
}

So, let's say i've made the getJSON that way:

$.getJSON('test.json', function(data){
   alert(data["K-6608-1-0"]["Info"]["SVGFile"]);
})

Why this code doesn't return "46658.svg"? Where's the error?

Thanks in advance ^^

1
  • 2
    data["K-6608-1-0"] is an array containing one element which is an object, so is Info. You want data["K-6608-1-0"][0]["Info"][0]["SVGFile"] Commented Jan 14, 2013 at 17:32

3 Answers 3

3

K-6608-1-0 and Info are arrays, so you have to set the position.

alert(data["K-6608-1-0"][0]["Info"][0]["SVGFile"]);
                         ^          ^
Sign up to request clarification or add additional context in comments.

Comments

2

That's because data["K-6608-1-0"] is an array, so to access the property you want, first you have to access an element of this array bi its index (data["K-6608-1-0"][0]["Info"] is also an array):

$.getJSON('test.json', function(data){
    alert(data["K-6608-1-0"][0]["Info"][0]["SVGFile"]);
    //                       ^          ^
});

Comments

0
alert(data["K-6608-1-0"][0]["Info"]["SVGFile"]);
                        ^^^--- add this

you've got arrays nested in objects nested in arrays nested in.... The first K-whatever is actually an array. You'll probably have to do the same for deeper levels as well.

2 Comments

Info is an array as well.
You forgot a [0] after ["Info"], since it's an array too

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.