问题
i get a JSON response from a web service:
{"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":"
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.","SwitchOffDate":"31 December 2013","Events":{"MaximumProximity":6.864859435168742,"Items":[{"Name":"Test Event","Time":"Time","Url":"","Date":"Date","Address":"19a Boundary Street, Rushcutter...
so my question is, in android java how to ask for the key: CoverageSearchByLatLongResult
in ObjC in iphone , something like this:
NSDictionary *coverageResult = [response objectForKey:@"CoverageSearchByLatLongResult"];
so basically im parsing a dictionary with a key, but that result i need to put it inside other dictionary,
[correct me if wrong, in java dictionaries are called maps?]
how to do this?
thanks a lot!
回答1:
You should use the classes in the org.json package:
http://developer.android.com/reference/org/json/package-summary.html
With them you can do things like:
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 keys to values. As an example of their use:
Map map = new HashMap();
map.put("string key", 1);
map.put("another key", 3);
来源:https://stackoverflow.com/questions/8812152/android-json-response-key-value-parsing