0

I want to retrieve all the values based on a key value from JSON object.

here's my sample JSON:

 [{
   "zip":544,
   "type":"UNIQUE",
   "primary_city":"Holtsville",
   "acceptable_cities":"",
   "unacceptable_cities":"Irs Service Center",
   "state":"NY",
   "county":"Suffolk County",
   "timezone":"America/New_York",
   "area_codes":"631",
   "latitude":40.81,
   "longitude":-73.04,
   "world_region":"NA",
   "country":"US",
   "decommissioned":0,
   "estimated_population":0,
   "notes":""
 },
 {
   "zip":601,
   "type":"STANDARD",
   "primary_city":"Adjuntas",
   "acceptable_cities":"",
   "unacceptable_cities":"Colinas Del Gigante, Jard De Adjuntas, Urb San Joaquin",
   "state":"PR",
   "county":"Adjuntas",
   "timezone":"America/Puerto_Rico",
   "area_codes":"787,939",
   "latitude":18.16,
   "longitude":-66.72,
   "world_region":"NA",
   "country":"US",
   "decommissioned":0,
   "estimated_population":0,
   "notes":""
 }]

So based on my zip code as key, I want to retrieve all other values.

I had tried the same thing for a JSON object with single key-value pairs, but don't know how to do it for above JSON object.

Here's my successfully running code for single key-value pairs

import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONObject;

public class map {

    public static void main(String[] args) {
        String t = "{\"A\":\"A1\",\"B\":\"B1\",\"C\":\"C1\"}";

        HashMap<String, String> map = new HashMap<String, String>();
        JSONObject jObject = new JSONObject(t);
        Iterator<?> keys = jObject.keys();

        while( keys.hasNext() ){
            String key = (String)keys.next();
            String value = jObject.getString(key);
            map.put(key, value);
        }

        System.out.println("json : "+jObject);
        System.out.println("map : "+map.get("A"));

    }

}

Output:

json : {"A":"A1","B":"B1","C":"C1"}
map : A1

any suggestions of how to do it?

I had seen several previous answers but none of them addresses this issue?

4
  • is your JSON an array of objects? Commented May 5, 2016 at 9:17
  • yes..edited the JSON in my question for more clarity Commented May 5, 2016 at 9:20
  • Pls. check my answer that will help. Commented May 5, 2016 at 9:22
  • 1
    Instead of writing your own logic to convert json to map, you can use jackson objectmapper. Check this - mkyong.com/java/how-to-convert-java-object-to-from-json-jackson Commented May 5, 2016 at 9:24

1 Answer 1

1

you can do it something like this. at the end of the loop your map will have zip to JSONObject mapping.

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) {
        String json = "[{\n" +
                "   \"zip\":544,\n" +
                "   \"type\":\"UNIQUE\",\n" +
                "   \"primary_city\":\"Holtsville\",\n" +
                "   \"acceptable_cities\":\"\",\n" +
                "   \"unacceptable_cities\":\"Irs Service Center\",\n" +
                "   \"state\":\"NY\",\n" +
                "   \"county\":\"Suffolk County\",\n" +
                "   \"timezone\":\"America/New_York\",\n" +
                "   \"area_codes\":\"631\",\n" +
                "   \"latitude\":40.81,\n" +
                "   \"longitude\":-73.04,\n" +
                "   \"world_region\":\"NA\",\n" +
                "   \"country\":\"US\",\n" +
                "   \"decommissioned\":0,\n" +
                "   \"estimated_population\":0,\n" +
                "   \"notes\":\"\"\n" +
                " },\n" +
                " {\n" +
                "   \"zip\":601,\n" +
                "   \"type\":\"STANDARD\",\n" +
                "   \"primary_city\":\"Adjuntas\",\n" +
                "   \"acceptable_cities\":\"\",\n" +
                "   \"unacceptable_cities\":\"Colinas Del Gigante, Jard De Adjuntas, Urb San Joaquin\",\n" +
                "   \"state\":\"PR\",\n" +
                "   \"county\":\"Adjuntas\",\n" +
                "   \"timezone\":\"America/Puerto_Rico\",\n" +
                "   \"area_codes\":\"787,939\",\n" +
                "   \"latitude\":18.16,\n" +
                "   \"longitude\":-66.72,\n" +
                "   \"world_region\":\"NA\",\n" +
                "   \"country\":\"US\",\n" +
                "   \"decommissioned\":0,\n" +
                "   \"estimated_population\":0,\n" +
                "   \"notes\":\"\"\n" +
                " }]";
        Map<Integer, JSONObject> map = new HashMap<>();
        JSONArray array = new JSONArray(json);
        for (int i = 0; i < array.length(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
            map.put(jsonObject.getInt("zip"), jsonObject);
        }


    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes thanks, just a small correction in your code, use "i" instead of 0 in second last line
one more thing, how to convert from my whole array into this beautiful formatted string which you used in your code? since it's a huge data, i don't want to do it manually.
I use IntelliJ IDEA. I just copy pasted the whole thing and it converts it into escape character wherever needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.