0

How do you extract multiple values from a JSON complex object / multidimensional array?

{
    "Items": [{
        "node": {
            "titre": "myTitle",
            "representation": {
                "1": "09 Octobre 2012 - 19:30",
                "2": "10 Octobre 2012 - 20:30",
                "3": "11 Octobre 2012 - 19:30"
            },
            "photo": {
                "1": "photo_1.jpg",
                "2": "photo_2.jpg",
                "3": "photo_3.jpg",
                "4": "photo_4.jpg"
            }
        }
    }]
}

For now, I'm using:

$.getJSON(url, function (data) {
    $.each(data.Items, function (i, node) {
        var titre = node.node.titre;
        var representation = node.node.representation;
        var photo = node.node.photo;
    }
    });

The result for titre is good, but for representation and photos, it renders:[object Object].

2
  • What language are you working in? Commented Oct 12, 2012 at 14:44
  • Looks like javascript with jQuery. Commented Oct 12, 2012 at 14:45

2 Answers 2

2

That is because Your photo here is an object ..

To access the values inside it you would need to use the keys 1 , 2 ,3, 4

 So var photo = node.node.photo;  // Is an Object 

 var photo1 = node.node.photo["1"] // photo_1.jpg
   var photo2 = node.node.photo["2"] // photo_2.jpg
   var photo3 = node.node.photo["3"] // photo_3.jpg

// same logic applies for representation

 var representation1 = node.node.representation["1"] // 09 Octobre 2012 - 19:30
   var representation2 = node.node.representation["2"] // 10 Octobre 2012 - 20:30
   var representation3 = node.node.representation["3"] // 11 Octobre 2012 - 19:30

To iterate over this you can try this

$.each(node.node.photo , function(key , value){

    console.log( value);
});

Check FIDDLE

EDIT

entry = {
                representation :[ sps.node.representation["1"],sps.node.representation["2"],sps.node.representation["3"] ],
                title: sps.node.titre,
                image: sps.node.image,
                acteurs: sps.node.acteurs,
                acteursuite: sps.node.acteurs_suite,
                lien: sps.node.lien,
                description: sps.node.corps,
                pics: sps.node.photo
            };

for( var i = 0; i< entry.representation.length ; i++){

     alert(entry.representation[i]);
}

// FOR FIDDLE

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

3 Comments

Unfortunately, my case is complicated: jsfiddle.net/elektrorl/QQhU7 How could I store the values "iterate way" in the "entry" variable, in my example?
It is already in an object format right.. If you want to still condense it you can store the representation in an array.. Check Edit
Wow, I didn't know we could condense like this. In fact, it's logical. But I'm trying something without result on the jsfddle, how could I create the loop to generate representation automatically without the keys?
0

Photo property is not an array. It is an object. The property value should be enclosed withim [ ].

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.