0

i am creating a web app. in which i need to parse a json which is having dynamic values.

 ResultSet rs = stmt.executeQuery(query);
    int  i=0;
        while(rs.next())
        {
            i++;
            object.put("qno"+i,rs.getInt(1));
            object.put("qname"+i,rs.getString(2));
            object.put("opA"+i,rs.getString(3));
            object.put("opB"+i,rs.getString(4));
            object.put("opC"+i,rs.getString(5));
            object.put("opD"+i,rs.getString(6));
            object.put("answer"+i,rs.getString(7));
        }

i am getting JSON like this... {"qno1":"1","qname":"asdgdfh","qno2":"2" ........ .... .... "qno20":"20"......}

how can i get the values by parsing the json using jQuery.

Thanks !

3 Answers 3

2
var obj = jQuery.parseJSON('{"name1":"John","name2":"Mary"}');
alert( obj.name1 === "John" );

with foreach-loop:

for (var o in obj) {
    alert(o + " " + obj[o]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

i have json like this {"name1":"john","name2":"adil","name3":"vikas"......} and i want to access these values inside a loop without having to write this obj.name1 or obj.name2 explicitly...
If you want use loop check my example - I've just added it
1

Why do you want dynamic object names? Seems like that would make all the code you use to deal with it more complicated. Doable, by why not just pass the number you're adding to the object name as value, and use an array of objects. E.g. create objects that look like this

{ "qno": 
    [{"id":1,"value":"somedata"},
     {"id":2,"value":"somedata"},
     ...],
  "qname": ... 
}

Comments

0

Assuming this has to be completely dynamic...

 json = {"a1" : "one", "a2" : "two"}

 for (var key in json){
    // key = key
    // value = json[key]
 }

1 Comment

I'm not sure what more detail you need. What are you trying to do with these values?

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.