How to parse polylines points using Jackson?

6 views
Skip to first unread message

Computer Gig

unread,
Feb 19, 2020, 4:44:00 PM2/19/20
to jackson-user
I want to parse the entire JSON file that contains many JSONObjects "steps". I need to extract all polylines and their points. However, it returns only the very first value and nothing more. Is anything wrong with my loop or I just parse the JSON file wrongly? Thx

https://gist.github.com/Biseamon/dad9e9c5b91b68d4caebcfa8af7a7eb8




public void parseDirect(String response) {

        PolylineOptions polylineOptions = new PolylineOptions();
        List<LatLng> movements = new ArrayList<>();
        try{
            JSONObject json = new JSONObject(response);
            //Retrieve routes from response
            JSONObject jsonRoute = json.getJSONArray("routes").getJSONObject(0);
            //Retrieve legs from routes
            JSONObject legs = jsonRoute.getJSONArray("legs").getJSONObject(0);
            //Retrieve steps from legs
            JSONArray steps = legs.getJSONArray("steps");
            final int numSteps = steps.length();
            JSONObject step;
            //Retrieve points from steps
            for (int i = 0; i < numSteps; i++) {
                step = steps.getJSONObject(i);
                String pontos = step.getJSONObject("polyline").getString("points");
                movements.addAll(PolyUtil.decode(pontos));
            }

//            movementsTest.add(new LatLng(52.4634252,-1.8861262));
//            movementsTest.add(new LatLng(52.4761324,-1.8608113));
//            movementsTest.add(new LatLng(52.4634252,-1.8689613));

            polylineOptions.color(Color.RED);
            polylineOptions.width(10);
            polylineOptions.addAll(movements);

            mMap.clear();
            mMap.addPolyline(polylineOptions);
            Log.i("parser", "parseDirect: " + movements.toString());
        }catch(Exception ex){
            Log.d("DirectionErr",ex.getMessage());
        }

        //make use of movements object here
    }


Tatu Saloranta

unread,
Feb 19, 2020, 4:46:19 PM2/19/20
to jackson-user
Your problem appears to be that you are using some other JSON library
than Jackson.
It would make sense to either switch to using Jackson first, or reach
support forum(s) of that other library (org.json?)

-+ Tatu +-
> --
> You received this message because you are subscribed to the Google Groups "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/3dd22b7d-6d8b-47c2-95fe-7631c185885d%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages