how to get data from json array

  • Replies:1
chiranjib PAhari
  • Forum posts: 1

Oct 30, 2017, 3:05:49 PM via Website

 json----

   {"refersTo":["Accounts"]}


  hot to get Accounts in a string value?

Reply
Tri Maya Sari
  • Forum posts: 1

Nov 5, 2017, 2:23:36 PM via Website

I think it's a JSONObject with a JSONArray inside (cmiiw). So, to get "Accounts" in a string value is:

//assume
String json = "{\"refersTo\":[\"Accounts\"]}";

String result = null; //use try-catch to use (cmiiw)
try {
JSONObject object = new JSONObject(json); //firts, get the jsonObject
JSONArray array = object.getJSONArray("refersTo"); //get the array with the key "refersTo"
result = array.getString(0); //"Accounts" is an element with index 0 in this array
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(result); //print "Accounts"

Reply