0

How can i iterate my json string return from a Java file in JavaScript ?

My JSON string having the following hierarchy;

ResponseObject -> SId -> SeId -> QId-> List of data. I need to access that list of data by looping through it using JavaScript.

Nishant
  • 54,584
  • 13
  • 112
  • 127
user630209
  • 1,123
  • 4
  • 42
  • 93

2 Answers2

0

You can use jQuery to parse JSON String or alternatively, you can create a JSON Object using the javascript eval() function.

Once you have a JSON object created, you can use dot notation to traverse your json, like

var value = ResponseObject.SId.SeId.Qid;

Where the attributes from the dot notation are your JSON key in the json string.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • im havin a list of values in Qid . how can i loop thru that – user630209 Mar 08 '11 at 12:10
  • If it's an array, then iterate it like you usually do with an array in javascript. Remember, the object can be manipulated the same way like you do in javascript, nothing changes. – Buhake Sindi Mar 08 '11 at 12:23
0

Use this notation: myobjet["MyAttributeName"].

for( var SId in ResponseObject ) {

    for(var SeId in ResponseObject[SId] ) {

        for(var QId in ResponseObject[SId][SeId] ) {
            var value = ResponseObject[SId][SeId][QId];
        }

    }

}

I didn't test it, but it should work.

Localist
  • 1,094
  • 1
  • 8
  • 14