prashantgpt91
prashantgpt91

Reputation: 1795

Use JSON object in java Hashmap

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?

Upvotes: 0

Views: 2710

Answers (1)

Deendayal Garg
Deendayal Garg

Reputation: 5158

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);
        }


    }
}

Upvotes: 1

Related Questions