2012-01-10 86 views
1

我從一個Web服務的JSON響應:Android的JSON響應鍵值,解析

{"CoverageSearchByLatLongResult":{"HasTransmissionAreasWithSignal":true,"SwitchOffArea": 
{"OpenForVastApplications":false,"SeperateTileSetUrl":"http:\/\/myswitch.merged.stage.orchard.net.au\/geodatafiles\/SwitchOffArea\/442b8844-3c05-4548-9424- 
4fdafc1f3c62\/Seperate","HASStatus":"Future","HASStatement":"<p>The Household Assistance Scheme is not yet available in this switchover area. Eligible households will be sent a letter when the scheme opens in the Sydney area.<\/p>","SwitchOffDate":"31 December 2013","Events":{"MaximumProximity":6.864859435168742,"Items":[{"Name":"Test Event","Time":"Time","Url":"","Date":"Date","Address":"19a Boundary Street, Rushcutter... 

所以我的問題是,在android系統的Java如何要求的關鍵在於:在CoverageSearchByLatLongResult

在ObjC

iphone,這樣的事情:

NSDictionary *coverageResult = [response objectForKey:@"CoverageSearchByLatLongResult"]; 

所以基本上即時解析用鑰匙一本字典,但結果我需要把它放在其他的字典裏面,

[糾正我如果錯了,在java字典裏叫地圖嗎?]

怎麼做到這一點?

非常感謝!

回答

2

你應該在org.json包中的類:

http://developer.android.com/reference/org/json/package-summary.html

有了他們,你可以做這樣的事情:

try { 
    String jsonString = "YOUR JSON CONTENT"; 
    JSONObject obj = new JSONObject(jsonString); 
    JSONObject anotherObj = obj.getJSONObject("CoverageSearchByLatLongResult"); 
    boolean bool = anotherObj.getBoolean("HasTransmissionAreasWithSignal"); 

    JSONArray jsonArray = obj.getJSONArray("arrayKey"); 
    int i = jsonArray.getInt(0); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

的Java maps地圖鍵的值。作爲它們使用的例子:

Map<String, Integer> map = new HashMap<String, Integer>(); 
map.put("string key", 1); 
map.put("another key", 3);