android json响应键值,解析

时间:2012-01-10 23:35:31

标签: android json parsing

我从网络服务获得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

在iphone的ObjC中,类似这样:

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

所以基本上我用一个键解析一个字典,但结果我需要把它放在其他字典中,

[纠正我如果错了,在java字典中称为地图?]

怎么做?

非常感谢!

1 个答案:

答案 0 :(得分: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);