Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9169443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:52:48+00:00 2026-06-17T15:52:48+00:00

I would like to get lat and lng from my JSON file and add

  • 0

I would like to get “lat” and “lng” from my JSON file and add to one array that keeps GeoPoint. I was trying to do it like that but it is not working for me :

protected List<GeoPoint> JsonArray(){
        List<GeoPoint> endp = new ArrayList<GeoPoint>();

        try{
        JSONObject obj = new JSONObject(json);
        JSONArray steps = obj.getJSONArray("routes");
        for(int i=0;i<steps.length();i++){
            JSONObject temp = steps.getJSONObject(i);
            JSONObject ele = temp.optJSONObject("steps").optJSONObject("end_location");
            ele.getJSONObject("lat");
            ele.getJSONObject("lng");

            double lat = Double.parseDouble(ele.getJSONObject("lat").toString());
            double lng = Double.parseDouble(ele.getJSONObject("lng").toString());
            endp.add(new GeoPoint((int)(lat *1E6),(int)(lng * 1E6)));

        }



        }catch (JSONException e) {
            // TODO: handle exception
        }
        return endp;
    }

Here’s a part of how looks my JSON file:

{
       "routes" : [
          {
             "bounds" : {
                "northeast" : {
                   "lat" : 41.87999000000001,
                   "lng" : -87.615020
                },
                "southwest" : {
                   "lat" : 29.74674000000001,
                   "lng" : -95.361220
                }
             },
             "copyrights" : "Dane do Mapy ©2013 Google",
             "legs" : [
                {
                   "distance" : {
                      "text" : "1 085 mil",
                      "value" : 1746457
                   },
                   "duration" : {
                      "text" : "16 godz. 39 min",
                      "value" : 59955
                   },
                   "end_address" : "1362 Chenevert Street, Houston, Teksas 77003, Stany Zjednoczone",
                   "end_location" : {
                      "lat" : 29.750110,
                      "lng" : -95.36016000000001
                   },
                   "start_address" : "138-230 South Columbus Drive, Chicago, Illinois 60601, Stany Zjednoczone",
                   "start_location" : {
                      "lat" : 41.87999000000001,
                      "lng" : -87.62075000000002
                   },
                   "steps" : [
                      {
                         "distance" : {
                            "text" : "338 stóp",
                            "value" : 103
                         },
                         "duration" : {
                            "text" : "1 min",
                            "value" : 9
                         },
                         "end_location" : {
                            "lat" : 41.88090,
                            "lng" : -87.62069000000001
                         },
                         "html_instructions" : "Kieruj się \u003cb\u003eS Columbus Dr\u003c/b\u003e na \u003cb\u003epółnoc\u003c/b\u003e w stronę \u003cb\u003eE Monroe St\u003c/b\u003e",
                         "polyline" : {
                            "points" : "}tr~FtlxuOuA@w@@QMUA"
                         },
                         "start_location" : {
                            "lat" : 41.87999000000001,
                            "lng" : -87.62075000000002
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "0,2 mil",
                            "value" : 266
                         },
                         "duration" : {
                            "text" : "1 min",
                            "value" : 33
                         },
                         "end_location" : {
                            "lat" : 41.88086000000001,
                            "lng" : -87.61750000000001
                         },
                         "html_instructions" : "Skręć \u003cb\u003ew prawo\u003c/b\u003e w \u003cb\u003eE Monroe St\u003c/b\u003e",
                         "polyline" : {
                            "points" : "szr~FhlxuO?SAyA?_B@yA?iBAaB?WBE@C@A?C?A@A?C?y@?_@"
                         },
                         "start_location" : {
                            "lat" : 41.88090,
                            "lng" : -87.62069000000001
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "2,0 mil",
                            "value" : 3186
                         },
                         "duration" : {
                            "text" : "3 min",
                            "value" : 180
                         },
                         "end_location" : {
                            "lat" : 41.85320,
                            "lng" : -87.61470000000001
                         },
                         "html_instructions" : "Skręć \u003cb\u003ew prawo\u003c/b\u003e w \u003cb\u003eU.S. 41 S\u003c/b\u003e",
                         "polyline" : {
                            "points" : "kzr~FjxwuOpMOZ@`PO|ACvDC@?lDCZAfMGjAA`@Av@?^?P?N@P@j@LRFNDRJRHZRb@\\b@^nAjA@@JJXXb@b@z@v@RRp@`@RLr@Xn@Np@J`A@bBOp@Kv@K@?@?@A@?@?@?@?PEvBWxB]fDq@xA]jF_BTIj@QpGuBd@OBAf@SrDyAPGNG@AVKbA_@~@]|@]z@Y`F{AtCy@fHyB"
                         },
                         "start_location" : {
                            "lat" : 41.88086000000001,
                            "lng" : -87.61750000000001
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "0,4 mil",
                            "value" : 581
                         },
                         "duration" : {
                            "text" : "1 min",
                            "value" : 30
                         },
                         "end_location" : {
                            "lat" : 41.848390,
                            "lng" : -87.614670
                         },
                         "html_instructions" : "Zjedź \u003cb\u003eInterstate 55 S\u003c/b\u003e w kierunku \u003cb\u003eSaint Louis\u003c/b\u003e",
                         "polyline" : {
                            "points" : "omm~FzfwuOVDPAN?VGlBk@b@M@?JEDAVI`A[dBi@`A[z@UPGZGf@E\\Ab@@VDJBHBTFTHRJPNRNLL\\f@NXLTP\\BF"
                         },
                         "start_location" : {
                            "lat" : 41.85320,
                            "lng" : -87.61470000000001
                         },
                         "travel_mode" : "DRIVING"
                      },

Does anyone knows how can I do it in other way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:52:49+00:00Added an answer on June 17, 2026 at 3:52 pm

    Your variable JSONArray steps = obj.getJSONArray("routes"); does not represent the “steps” part of the JSON object, but rather the “routes” part.

    Also, you have skipped some of the levels in the JSON hierarchy. With the given JSON, you would need the following steps to traverse the JSON (I have omitted looping over the different arrays, except for the routes array)

    JSONArray routes = obj.getJSONArray("routes");
    for(int i=0; i < routes.length(); i++){
        // Grab the first route
        JSONObject route = routesArray.getJSONObject(i);
        // Take all legs from the route
        JSONArray legs = route.getJSONArray("legs");
        // Grab first leg
        JSONObject leg = legs.getJSONObject(0);
        // Take all steps from the leg
        JSONArray steps = leg.getJSONArray("steps");
        // Grab first step
        JSONObject step = steps.getJSONObject(0);
    
        JSONObject endLocation = step.getJSONObject("end_location");
        String lat = endLocation.getString("lat");
        String lng = endLocation.getString("lng");
    

    Reference: JSON parsing of Google Maps API in Android App

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get View instance that is used to display specific Preference
I would like to get a help from you all. The below is the
I have an array that I would like to iterate through with a for
I have a trip model that contains an array of lat/lng pairs class Trip
If I create a Polygon layer with points: [{lat:51.509,lng:-0.08},{lat:51.503,lng:-0.06},{lat:51.51,lng:-0.047},{lat:51.500,lng:-0.039}] , I get something that
I would like to get numbers of comments for each posts using mysql. I
I would like to get to know how to redirect output of some program
I would like to get the generic information ( Counter class) of my CounterPersistence
I would like to get rid of the close/X button on the top right
I would like to get started developing games with Flixel, but im on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.