Search code examples
javajsonxmlparsingoverpass-api

How to parse overpassAPI http response contents into variables


Http query to overpassAPI is working but I have tried many different approaches to programatically convert the response (very long string) into anything useful.

Tried line by line, substrings, etc in all manner of combinations but still cant find anything that works as I need it to:

take response string, find first "item" & store type (e.g 'way' or 'node'), then make variables (possibly string arraylist) of the names and values of each item element (eg [id, lat, lon, timestamp]... and [000001, 50, 50, 12:00,]... etc) so i can use them as output values to the user. There may not always be the same amount of items in the list for each item, hence i have been failing to use various loops to achieve this so far.

I have no preference in either XML or JSON response format as all I need are the values.

Can someone suggest code to achieve this? Or at least suggest somewhere that has the answers I'm looking for?

I've wasted nearly an entire day trying to figure it out myself and googling for a solution so far

Many thanks


Solution

  • hopefully this will save someone else the agonisizing headache this caused me: solved after about 7-8 hours of trial-and-error:

    
    public void responseParse(String response) {
        try {
            //get json object from (whole) response string
            JSONObject responseObj = new JSONObject(response);
            //pull out jsonArray (of elements) from object
            JSONArray responseArr = responseObj.getJSONArray("elements");
            Log.d(TAG, "responseParse: Elements: " + responseArr);
    
            //empty previous road list
            roadArrList = new ArrayList<>();
    
            //for each element item in json array
            for (int i = 0; i < responseArr.length(); i++) {
                //convert element to string of contents
                String elementStr = responseArr.getString(i);
                //create individual element object from (whole) element string
                JSONObject elementObj = new JSONObject(elementStr);
    
                //pass element (road) for road data string extraction (and adding to list)
                getCardData(elementObj);
    
            }
        } catch (Exception e) {
            Log.e(TAG, "responseParse: error: json parsing;");
            //todo: handle error
            e.printStackTrace();
        }
    
        //set recyclerView with road cards
        buildRecyclerView();
    }
    
    

    element object is passed to recyclerView building method:

    public void getCardData(JSONObject elementObj) {
            //pull out jsonObject (of tags) from element
            try {
                JSONObject tagObj = elementObj.getJSONObject("tags");
            } catch (JSONException e) {
                Log.e(TAG, "getCardData: error creating tag json object");
                e.printStackTrace();
            }
    
            //assign road name
            try {
                //for each element, get its required tags and get string of value
                roadName = elementObj.getJSONObject("tags").getString("name");
                Log.d(TAG, "responseParse: ROADNAME RETRIEVED: " + roadName);
            } catch (JSONException e) {
                Log.w(TAG, "responseParse: JSON exception occurred: no NAME for road");
                roadName = "NO ROAD NAME AVAILABLE";
            }
    
            //assign road ID
            try {
                roadId = elementObj.getString("id");
                Log.d(TAG, "responseParse: ID RETRIEVED: " + roadId);
            } catch (JSONException e) {
                Log.w(TAG, "responseParse: JSON exception occurred: no ID for road");
                roadId = "NO ROAD ID AVAILABLE";
            }
    
            //assign road limit
            try {
                roadLimit = elementObj.getJSONObject("tags").getString("maxspeed");
                Log.d(TAG, "responseParse: SPEED RETRIEVED: " + roadLimit);
            } catch (JSONException e) {
                Log.w(TAG, "responseParse: JSON exception occurred: no SPEED for road");
                roadLimit = "NO ROAD LIMIT AVAILABLE";
            }
    
            //add element (road) values into array (new card)
            roadArrList.add(new CardRoad(R.drawable.ic_road, roadName, roadId, roadLimit));
        }