2016-05-05 46 views
0

我想根据JSON对象的键值检索所有值。在java中使用JSON对象Hashmap

这里是我的样品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":"" 
}] 

因此,基于我的邮政编码为重点,我想要检索的所有其他值。

我曾尝试过使用单个键值对的JSON对象,但不知道如何为上面的JSON对象做同样的事情。

这是我的一个键 - 值对

成功运行代码
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")); 

    } 

} 

输出:

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

的如何做到这一点有什么建议?

我已经看过几个以前的答案,但没有一个解决这个问题?

+0

是你的JSON对象数组? –

+0

yes..edited JSON在我的问题更清晰 – x0v

+0

请。检查我的答案,这将有助于。 –

回答

1

你可以这样做。在循环结束时,您的地图将具有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); 
     } 


    } 
} 
+0

是的,谢谢,只是在你的代码中的一个小的更正,使用“我”代替第二行最后一行的编号为 – x0v

+0

已编辑。谢谢。 –

+0

还有一件事,如何从我的整个数组转换成您在代码中使用的美丽格式化字符串?因为它是一个庞大的数据,我不想手动完成。 – x0v