2011-08-26 80 views
0

這是來自我的服務器的json響應。我如何解析它並將其存儲在HashMap中?請幫幫我。如何解析此JSON對象

{'records': 
    [{ 
     'number':165, 
     'description': 'abcd' 
     }, 
     { 
     'number':166, 
     'description': 'ab' 
     }, 
     { 
     'number':167, 
     'description': 'abc' 
     }] 
    } 
+0

你使用可以提供一個JSON解析器標準庫功能的編程語言完全教程,但因爲你是不透露你的語言使用,這很難幫助你。 –

+5

提出17個問題,而不是接受單個答案?真??請在提出更多問題之前解決此問題。 –

+0

[在Android中發送和解析JSON]可能的重複(http://stackoverflow.com/questions/2818697/sending-and-parsing-json-in-android) –

回答

1

即時通訊新這樣的:

JSONObject JsonObject = new JSONObject(json); 
JSONArray JsonArray_ = JsonObject .getJSONArray("records"); 

for (int i = 0; i < numberOfItems; i++) { 
JSONObject record= JsonArray_photo.getJSONObject(i);  
parsedObject.number = record.getString("number"); //its the same for all fields   
map.add(parsedObject); 
} 

我做過類似的東西爲我自己的JSON解析器。希望這可以幫助。歡呼聲

0

我建議你看一下gson庫,這很容易將JSON解析爲代表數據的對象。因此,您可以創建一個Record對象,其中包含用於數字和描述的公共成員變量,然後使用gson將JSON解析爲對象數組。

http://code.google.com/p/google-gson/

將.jar添加到您的libs文件夾中,然後使用它是這樣的:

Record[] records = new GsonBuilder().create().fromJson(jsonString,Record[].class) 
int number = record[0].number; 
0

org.json包包含在Android版本:0​​

使用簡單:

JSONObject json_object = new JSONObject(String json_string); 

各屬性然後y可以作爲Java屬性來訪問,例如, json_object.property。如果由於某種原因解析失敗(即無效的JSON),它將拋出JSONException。在android系統,但也許你可以做一些事情