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

  • Home
  • SEARCH
  • 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 8579463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:36:42+00:00 2026-06-11T20:36:42+00:00

I made a simple JSON parser using this . All I want to do

  • 0

I made a simple JSON parser using this. All I want to do is parse the name and lat/lng of businesses with the Google maps api, but I cant seem to get it to parse correctly.

Example of JSON results:

"results" : [
  {
     "geometry" : {
        "location" : {
           "lat" : 41.8881090,
           "lng" : -87.6308430
        }
     },
     "name" : "American Bar Association",
  },
  {
     "geometry" : {
        "location" : {
           "lat" : 41.8721230,
           "lng" : -87.6294680
        }
     },
     "name" : "Bar Louie",
   
  } ], "status" : "OK"}

I took out some of the other details Im not interested in to shorten the example, but here’s my code:

            JSONParser jParse = new JSONParser();
            JSONObject json = jParse.getJSON(url);
    
            try
            {   
                JSONArray contacts = json.getJSONArray("results"); //Parent Node
                for(int i = 0; i < 5; i++)//Loop through first 5 results results            
                {
                    JSONObject c = contacts.getJSONObject(i);
                    name = c.getString("name");//Gets name field 
                    
                    //get lat from location which is a sub object of geometry                       
                    lat = Float.valueOf (contacts.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getString("lat")); 
                }
            } catch (JSONException e) { e.printStackTrace(); }

When I run it I get the first result just fine, I get the correct Name and Latitude but instead of moving to the second result it moves to the last result of i and prints its for the rest of the loop. Hard to explain, what I mean is if I loop through 5 results I get:

  • Loop 1 Output: Result#1 //This is good
  • Loop 2 Output: Results#5 //Somehow we skip to result #5 instead of getting #2
  • Loop 3 Output: Results#5 //Now it just keeps printing #5 each time through the rest of the loop
  • Loop 4 Output: Results#5
  • Loop 5 Output: Results#5

I have no idea how its just ignoring the for loop and getting to the last result? Im printing the result with just a Toast message which I left out for a shorter snippet.

(I will note I am running this through a separate thread and using a handler to display the Toast message, if that matters)

I’m really not that great in Java and the it’s the first time I’ve worked with JSON so spare any stupidity I may have said. Any help is much appreciated. 🙂

  • 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-11T20:36:43+00:00Added an answer on June 11, 2026 at 8:36 pm

    I ended up answering my own problem. I think it was because I re-used the name variable each time through the loop and by the time the handler kicked in and finished displayed the first message the loop had already finished and assigned name the last value. So when the other 4 handler requests finally got executed it just printed the same value 4 times.

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

Sidebar

Related Questions

hi i've got question i've made simple form with attachment input and i want
OK, I have made simple rectangular using OpenGL, and it looks pretty simple glNormal3f(0.0f,
On Windows, testing different OSes is made simple using VMs. Is there a simple
I made a simple clock signal using goroutines: func clockloop(ch chan byte) { count
I made a simple javascript class like this: function Horse() { this.color = 'brown';
I'm a beginner with JAX RS architecture. I've made a simple class like this:
I made a very simple wordpress plugin that prints some posts in JSON format.
Hay guys, i've made a simple drawing application using the canvas tag. However i
I made a simple sample on how to insert using AJAX and retrieving it
I've made this very simple jsfiddle to show you this abnormal behaviour http://jsfiddle.net/mrgamer/GgUkE/2/ In

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.