tags:

views:

54

answers:

1

I'm getting this json string info from the server :

{"members":[["sd2840d","Johny"],["jkld341","Marry"]]}

So I store in in variable :

js = "{\"members\":[[\"sd2840d\",\"Johny\"],[\"jkld341\",\"Marry\"]]}";

and create json object

json = new JSONObject(js);

Naturally I have many occurences of members, each member has something like identifier sd2840d and name Johny both strings, how can I create for loop or foreach loop that will print out .. this is identifier sd2840d and this is name Johny, so on for Marry etc. tnx

+1  A: 
JSONObject json = new JSONObject(
    "{\"members\":[[\"sd2840d\",\"Johny\"],[\"jkld341\",\"Marry\"]]}");

JSONArray array = json.getJSONArray("members");

for (int idx = 0; idx != array.length(); idx++) {
  JSONArray array2 = array.getJSONArray(idx);
  System.out.println(array2.getString(0));
  System.out.println(array2.getString(1));
}
Jim Blackler
@Jim Blackler thank you , I get this error msg :`Error: A JSONObject text must begin with '{' at character 1`
Gandalf StormCrow
Funny, are you using 'org.json.JSONObject'?
Jim Blackler
@Jim Blackler Yes and array `import org.json.JSONArray;import org.json.JSONObject;`
Gandalf StormCrow
This error is on the first line? I have run this code and it does work for me.
Jim Blackler
works here for me, so maybe you did an error copying
Valentin Rocher