1

I am new at programming. I have this problem of fetching the values from sub array entitled "subresult". I have code of getting values from an array but it seems that it only gets the outer array and disregard the inner array. It is sad that I have no luck in this. Please respect my question because I'm just a beginner. Hoping an answer from you guys. Thank you!

JSON:

{  
   "result":[  
      {  
         "commonname":"Tamaraw",
         "subresult":[  
            {  
           "latitude":"13.088847376649245",
           "longitude":"121.0535496566772",
           "province":"Mindoro"
        },
        {  
           "latitude":"14.898346071682198",
           "longitude":"121.42616213302608",
           "province":"General nakar"
        },
        {  
           "latitude":"14.44133541176629",
           "longitude":"120.45936525802608",
           "province":"Bataan"
        }
     ]
  },
  {  
     "commonname":"Philippine Tarsier",
     "subresult":[  
        {  
           "latitude":"9.810806171435631",
           "longitude":"124.26143093023506",
           "province":"Bohol"
        }
     ]
  },
  {  
     "commonname":"Philippine Eagle",
     "subresult":[  
        {  
           "latitude":"7.2396901503428",
           "longitude":"125.44315069027664",
           "province":"Davao City"
        }
     ]
  },
  {  
     "commonname":"Visayan Warty Pig",
     "subresult":[  
        {  
           "latitude":"9.651642644962154",
           "longitude":"122.84131398239595",
           "province":"Panay And Negros"
        }
     ]
  },
  {  
     "commonname":"Philippine Fresh Water Crocodile",
     "subresult":[  
        {  
           "latitude":"13.093068957060206",
           "longitude":"121.06598892373722",
           "province":"Mindoro"
        }
     ]
  },
  {  
     "commonname":"Walden's Hornbill",
     "subresult":[  
        {  
           "latitude":"10.071930427284427",
           "longitude":"125.59779391691245",
           "province":"Camiguin Sur and Dinagat Island"
        },
        {  
           "latitude":"14.656674396646768",
           "longitude":"121.05812014083858",
           "province":"Quezon"
        }
     ]
  },
  {  
     "commonname":"Philippine Cockatoo",
     "subresult":[  
        {  
           "latitude":"9.380944735295179",
           "longitude":"118.38456063371927",
           "province":"Palawan"
        },
        {  
           "latitude":"15.491670747921509",
           "longitude":"120.94052188562534",
           "province":"Cabanatuan City"
        },
        {  
           "latitude":"15.378556159943873",
           "longitude":"121.39594973068233",
           "province":"Dingalan"
        }
     ]
  },
  {  
     "commonname":"Negros Bleeding-heart",
     "subresult":[  
        {  
           "latitude":"9.551081019434731",
           "longitude":"123.09185859510103",
           "province":"Negros and Panay"
        }
     ]
  },
  {  
     "commonname":"Philippine naked-backed fruit bat",
     "subresult":[  
        {  
           "latitude":"9.646007526813666",
           "longitude":"122.85255472090171",
           "province":"Negros"
        }
     ]
  },
  {  
     "commonname":"Philippine Forest Turtle",
     "subresult":[  
        {  
           "latitude":"9.35368808473656",
           "longitude":"118.36544272849846",
           "province":"Palawan"
        }
     ]
  },
  {  
     "commonname":"Dinagat bushy-tailed Cloud Rat",
     "subresult":[  
        {  
           "latitude":"10.100726050965035",
           "longitude":"125.59963979398412",
           "province":"Dinagat Island"
        }
     ]
  },
  {  
     "commonname":"Hawksbill Sea Turtle",
     "subresult":[  
        {  
           "latitude":"6.984796116278719",
           "longitude":"122.02642351890961",
           "province":"Zamboanga"
        }
     ]
  },
  {  
     "commonname":"Philippine Spotted Deer",
     "subresult":[  
        {  
           "latitude":"16.229997551983594",
           "longitude":"120.52623997214562",
           "province":"Baguio"
        }
     ]
  }
   ]
}

Java code:

  public JSONArray result;
  public static final String JSON_ARRAY = "result";
  public static final String TAG_COMMONNAME= "commonname";
  public void getData(){

    //Creating a string request
    StringRequest stringRequests = new StringRequest(Config.DATA_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject jo = null;
                    try {
                        //Parsing the fetched Json String to JSON Object
                        jo = new JSONObject(response);

                        //Storing the Array of JSON String to our JSON Array
                        result = jo.getJSONArray(JSON_ARRAY);

                        //Calling method getStudents to get the students from the JSON Array
                        getAnimals(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //Creating a request queue
    RequestQueue requestQueue2 = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue2.add(stringRequests);
}

public void getAnimals(JSONArray ja){
    //Traversing through all the items in the json array
    for(int i=0;i<ja.length();i++){
        try {
            //Getting json object
            json = ja.getJSONObject(i);

            //Adding the name of the student to array list
            students.add(json.getString(TAG_COMMONNAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
7
  • everything looks good but what is your problem ? Commented Mar 29, 2019 at 11:06
  • Don't try to handle the maps and arrays in json data by hand, use something like ObjectMapper to map it to a proper POJO. Commented Mar 29, 2019 at 11:07
  • Hi there! I am fetching the value of the array but it seems that the sub array are not being called. Can u help me? Commented Mar 29, 2019 at 11:14
  • Can you elaborate on this matter? What is it that you want? Commented Mar 29, 2019 at 11:26
  • 1
    then just json.getJSONArray("subresult"); same like you did with json.getString(TAG_COMMONNAME); Commented Mar 29, 2019 at 11:40

1 Answer 1

1
public void getAnimals(JSONArray ja){
    //Traversing through all the items in the json array
    for(int i=0;i<ja.length();i++){
        try {
            //Getting json object
            json = ja.getJSONObject(i);

            //Adding the name of the student to array list
            students.add(json.getString(TAG_COMMONNAME));

            //TRY THIS
            JsonArray subResultJsonArray = json.optJSONArray("subresult");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

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.