简体   繁体   English

在Java Hashmap中使用JSON对象

[英]Use JSON object in java Hashmap

I want to retrieve all the values based on a key value from JSON object. 我想基于JSON对象的键值检索所有值。

here's my sample JSON: 这是我的示例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. 我曾对具有单个键值对的JSON对象尝试过相同的操作,但不知道如何为上述JSON对象执行此操作。

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? 我以前看过几个答案,但是都没有解决这个问题?

you can do it something like this. 你可以做这样的事情。 at the end of the loop your map will have zip to JSONObject mapping. 在循环结束时,您的地图将具有zip到JSONObject映射的邮政编码。

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


    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM