Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help with json parsing

Tags:

java

json

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

like image 912
Gandalf StormCrow Avatar asked Jan 01 '26 04:01

Gandalf StormCrow


1 Answers

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));
}
like image 71
Jim Blackler Avatar answered Jan 02 '26 17:01

Jim Blackler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!