Sexyrexi Derfanjoko Sexyrexi Derfanjoko - 1 year ago 98
Java Question

getting deeper into the output from a http request

I have made an autoCompleteTextView that should show some values from a very complexe json file. I donĀ“t know how to get so "deep" into the directory of the json. The values I want to have are nested into the code. The code comes from an http request. The other solutions in this forum didn`t really help me :(

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

public class JsonParse {
double current_latitude,current_longitude;
public JsonParse(){}
public JsonParse(double current_latitude,double current_longitude){
public List<SuggestGetSet> getParseJsonWCF(String sName)
List<SuggestGetSet> ListData = new ArrayList<SuggestGetSet>();
try {
URL js = new URL("something");
URLConnection jc = js.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
String line = reader.readLine();
JSONObject jsonResponse = new JSONObject(line);
JSONArray jsonArray = jsonResponse.getJSONArray("something");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject r = jsonArray.getJSONObject(i);
ListData.add(new SuggestGetSet(r.getString("something"),r.getString("something")));
} catch (Exception e1) {
// TODO Auto-generated catch block
return ListData;


for example a json file like this:

{"id":1,"error":null,"result":{"nPeople":2,"people":[{"namePeople":"Inca","power":"1235","location":"asdfghjja","idPeople":189,"mainItems":"brownGem","verified":false,"description":"Lorem impsum bla bla","linkAvatar":"avatar_12.jpg","longitude":16.2434263,"latitude":89.355118},{"namePeople":"Maya","power":"1235","location":"hcjkjhljhl","idPeople":119,"mainItems":"greenstone","verified":false,"description":"Lorem impsum bla bla","linkAvatar":"avatar_6.jpg","longitude":16.2434263,"latitude":89.3551185}]}}

Answer Source

To get "deeper" into a JSON structure you just have to get the sub element(s) from your JSONObject in question.

JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonFormattedVehicleResponse);
JSONArray vehicles = (JSONArray) jsonObject.get("vehicle");
for(Object vehicle : vehicles) {
    JSONObject vehicleJSON = (JSONObject) vehicle;
    if(vehicleJSON.containsKey("subValue")) {
        JSONObject subJSON = (JSONObject) vehicleJSON.get("subValue");
        //do stuff

Rinse and repeat for each subelement you have to get to, all the way down to the information you're looking for. Although, this does require that you're familiar with the JSON you're getting in the request.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download