-4

json values in toast typein my project getting data from server side in that i'm sending array with nested array in response. in this image you can see the "Secondary_number"with array has some values. i need to print those value. im attached my java code . when i try to run this my screen get black.

JSONObject obj = new JSONObject(response);
JSONArray jsonArray = obj.getJSONArray("response");
service = new String[jsonArray.length()];
//Iterate the jsonArray and print the info of JSONObjects
for (int i = 0; i < jsonArray.length(); i++) {
     JSONObject jsonObject = jsonArray.getJSONObject(i)
//getting second array
      JSONArray jArray = jsonObject.getJSONArray("secondary_number");

    //Iterate the jsonArray and print the info of JSONObjects
      for (int j = 0; j < jArray.length(); j++) { 
              do some computation
}
  service[i] = jsonObject.getString("service_id").toString(); //getting values in response 
  tv1 = jsonObject.getString("service_id");
  tv2 = jsonObject.getString("user_id").toString();
  tv3 = jsonObject.getString("username").toString();
  tv4 = jsonObject.getString("company_name").toString();
  tv5 = jsonObject.getString("assigned_time").toString();

so kindly help out from this

pradeep
  • 45
  • 7

1 Answers1

0

The two first lines are ok

JSONObject obj = new JSONObject(response);
JSONArray services = obj.getJSONArray("response");

But after that, it is not. I don't know what you want to do, probably for debugging purposes so i will not do anything with it. Also, i will get the strings in order to make it clear.

for(int i = 0; i < services.length(); i++){
    JSONObject service = services.getJSONObject(i);

    tv1 = service.getString("service_id");
    tv2 = service.getString("assigned_time");
    tv3 = service.getString("company_name");
    tv4 = service.getString("user_id");
    tv5 = service.getString("username");
    //for some reason, secondary_number value is escaped which mean 
    //it is a string that you will have to parse
    String secondary_number = service.getString("secondary_number");
    JSONArray secondary_number_json = new JSONArray(secondary_number);

    for(int j = 0; j < secondary_number_json.length(); j++){
        JSONObject secondary = secondary_number_json.getJSONObject(j);
        String name = secondary .getString("name");
        String alternative_no = secondary .getString("alternative_no");
        String designation = secondary .getString("designation");
        // do things with those strings
    }
}

For a better usage of the data, you should look at Gson library to parse json more easily with pojo classes

Kilarn123
  • 723
  • 5
  • 6