2013-05-14 27 views
0

我創建了一個JSON編碼,您在其中輸入HashTable(public Hashtable<?, ?> JSonDecode(String data) {... return objJS.toString(); })並獲取JSON格式的字符串。那就是:解析JSON中的哈希表,完美(jsonencode)。但我不知道如何解析Hashtable(jsondecode)中的json。 (Android)

如果我有這個(哈希表中的哈希表)一個Hashtable:

例Hashtable的

Hashtable<String, Object> exampleHT = new Hashtable<String, Object>(); 
    exampleHT.put("Color", "Red"); 
    exampleHT.put("OtherKey", "OtherValue"); 
    exampleHT.put("OtherKey2", "OtherValue2"); 

Hashtable<String, Object> country = new Hashtable<String, Object>(); 
    country.put("Spain", "Madrid"); 
    country.put("France","Paris"); 
    country.put("Italy", "Rome"); 

Hashtable<String, String> pokemon = new Hashtable<String, String>(); 
    pokemon.put("Pikachu", "Electric"); 
    pokemon.put("Charmander","Fire"); 

country.put("Pokemons", pokemon); 

exampleHT.put("Countries", country); 

我用我的功能(JSonEncode(exampleHT);),我得到這個字符串:

{ 
    "Color":"Red", 
    "Countries":{ 
     "Spain":"Madrid", 
     "France":"Paris", 
     "Italy":"Rome", 
     "Pokemons":{ 
      "Pikachu":"Electric", 
      "Charmander":"Fire" 
     } 
    }, 
    "OtherKey":"OtherValue", 
    "OtherKey2":"OtherValue2" 
} 

它完美的工作!我的問題是創建相反的過程,與JSonDecode

Hashtable<?, ?> hashUnknown = JSonDecode(jsonStringExample); 

public Hashtable<?, ?> JSonDecode(String data) { 
// I do not know how to parse json in Hashtable, without indicating the tags manually. 

}

我不知道如何解析JSON在哈希表,無需手動指示標記。 也就是說,沒有它:

JSONArray menuObject = new JSONArray (jObject.getString ("Color")); 
JSONArray menuObject = new JSONArray (jObject.getString ("Countries")); 

這應該是不知道的JSON動態內容,而無需手動編寫顏色,國家,....

任何意見或建議嗎?謝謝,

回答

1

你可以在你的JSONObject(jObject)的密鑰的Iterator對象(java.util.Iterator的) 所以,你可以寫這樣的事情:

Iterator<String> it = jObject.keys(); 
String key = null; 
Object value = null; 
while (it.hasNext()) { 
    key = it.next(); 
    value = jObject.get(key); 
    // Then test the instance of the value variable 
    // and perform some logic 
}