2016-05-05 83 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