2
{
"elements":[{
        "content":{
            "xyz-media:game":{

                "display_name":"Pool Practice",
                "enable_video_ad":true,

            }
    },
    {
        "content":{
            "xyz-media:game":{

                "display_name":"Bouncing Balls: Christmas Edition",
                "enable_video_ad":true,

            }
    },

    ]  
}

How do i parse this JSON String? I have been trying this for last 2 days without any luck. Below is the code that I have been trying.

//where r.responseText is the Response from URL
messages = JSON.parse(r.responseText);

//To access the display_name of first content
messages.elements[0].content[0].display_name

Please tell me where am i going wrong?

1 Answer 1

2

Elements is an array of maps . That is element[0] is a map with one of ther keys as content and the value of content is a map again, So there isn't any elements[0].content[0]. You access it as element[0]["content"] or element[0].content but note there is no [0] for content, as it is not an array, and you proceed so on to access other values.

1
  • To access display_name : elements[0].content["xyz-media:game"].display_name; Commented Feb 10, 2012 at 6:53

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.