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 8676651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:15:02+00:00 2026-06-12T20:15:02+00:00

This is my below code from which I need to parse the JSONObject to

  • 0

This is my below code from which I need to parse the JSONObject to get individual items. This is the first time I am working with JSON. So not sure how to parse JSONObject to get the individual items from JSONObject.

try {
    String url = service + version + method + ipAddress + format;
    StringBuilder builder = new StringBuilder();
    httpclient = new DefaultHttpClient();
    httpget = new HttpGet(url);
    httpget.getRequestLine();
    response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream inputStream = entity.getContent();
        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        for (String line = null; (line = bufferedReader.readLine()) != null;) {
            builder.append(line).append("\n");
        }
        JSONObject jsonObject = new JSONObject(builder.toString());
        // Now iterate jsonObject to get Latitude,Longitude,City,Country etc etc.

    }

} catch (Exception e) {
    getLogger().log(LogLevel.ERROR, e.getMessage());
} finally {
    bufferedReader.close();
    httpclient.getConnectionManager().shutdown();
}

My JSON looks like this:

{
    "ipinfo": {
        "ip_address": "131.208.128.15",
        "ip_type": "Mapped",
        "Location": {
            "continent": "north america",
            "latitude": 30.1,
            "longitude": -81.714,
            "CountryData": {
                "country": "united states",
                "country_code": "us"
            },
            "region": "southeast",
            "StateData": {
                "state": "florida",
                "state_code": "fl"
            },
            "CityData": {
                "city": "fleming island",
                "postal_code": "32003",
                "time_zone": -5
            }
        }
    }
}

I need to get latitude, longitude, city, state, country, postal_code from the above object. Can anyone provide any suggestion how to do it efficiently?

  • 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-12T20:15:04+00:00Added an answer on June 12, 2026 at 8:15 pm

    You can try this it will recursively find all key values in a json object and constructs as a map . You can simply get which key you want from the Map .

    public static Map<String,String> parse(JSONObject json , Map<String,String> out) throws JSONException{
        Iterator<String> keys = json.keys();
        while(keys.hasNext()){
            String key = keys.next();
            String val = null;
            try{
                 JSONObject value = json.getJSONObject(key);
                 parse(value,out);
            }catch(Exception e){
                val = json.getString(key);
            }
    
            if(val != null){
                out.put(key,val);
            }
        }
        return out;
    }
    
     public static void main(String[] args) throws JSONException {
    
        String json = "{'ipinfo': {'ip_address': '131.208.128.15','ip_type': 'Mapped','Location': {'continent': 'north america','latitude': 30.1,'longitude': -81.714,'CountryData': {'country': 'united states','country_code': 'us'},'region': 'southeast','StateData': {'state': 'florida','state_code': 'fl'},'CityData': {'city': 'fleming island','postal_code': '32003','time_zone': -5}}}}";
    
        JSONObject object = new JSONObject(json);
    
        JSONObject info = object.getJSONObject("ipinfo");
    
        Map<String,String> out = new HashMap<String, String>();
    
        parse(info,out);
    
        String latitude = out.get("latitude");
        String longitude = out.get("longitude");
        String city = out.get("city");
        String state = out.get("state");
        String country = out.get("country");
        String postal = out.get("postal_code");
    
        System.out.println("Latitude : " + latitude + " LongiTude : " + longitude + " City : "+city + " State : "+ state + " Country : "+country+" postal "+postal);
    
        System.out.println("ALL VALUE " + out);
    
    }
    

    Output:

        Latitude : 30.1 LongiTude : -81.714 City : fleming island State : florida Country : united states postal 32003
    ALL VALUE {region=southeast, ip_type=Mapped, state_code=fl, state=florida, country_code=us, city=fleming island, country=united states, time_zone=-5, ip_address=131.208.128.15, postal_code=32003, continent=north america, longitude=-81.714, latitude=30.1}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am able to provide animation to an image button from below code. this.RegisterName(image1.Name,
Given below is program for encrypting a string. I had taken this code from
I'm working on a rake task which imports from a JSON feed into an
i am using the below json method and following code to parse json method
How can I extract this variable bellow from a website to my android code?
I have this below code and it work fine header (content-type: text/xml); $xml =
This code below allows me to find the word error in all my files
This code below is for executing ls -l | wc -l. In the code,
This code below works in all web browsers except IE: <input type="text" name="passwordLogin" value="Password"
This code below doesn't work correctly since my MFC program is in unicode circumstance.

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.