Json: How to extract inner Json objects from a single outer Json object using java
I have the following Json string, resulting from a google search query:
{"responseData":{"results":[{"region":"IL","streetAddress":"1611 South Randall Road","titleNoFormatting":"Brunswick Zone XL Randall Road","staticMapUrl":"http:\/\/maps.google.com\/maps\/api\/staticmap?maptype=roadmap&format=gif&sensor=false&size=150x100&zoom=13&markers=42.162958,-88.334155","listingType":"local","addressLines":["1611 South Randall Road","Algonquin, IL"],"lng":"-88.334155","phoneNumbers":[{"type":"","number":"(847) 658-2257"}],"url":"http:\/\/www.google.com\/maps\/place?source=uds&q=brunswick+zone&cid=8286591317090502839","country":"United States","city":"Algonquin","content":"","GsearchResultClass":"GlocalSearch","maxAge":604800,"title":"<b>Brunswick Zone<\/b> XL Randall Road","ddUrlToHere":"http:\/\/www.google.com\/maps?source=uds&daddr=1611+South+Randall+Road,+Algonquin,+IL+(Brunswick+Zone+XL+Randall+Road)+@42.162958,-88.334155&iwstate1=dir:to","ddUrl":"http:\/\/www.google.com\/maps?source=uds&daddr=1611+South+Randall+Road,+Algonquin,+IL+(Brunswick+Zone+XL+Randall+Road)+@42.162958,-88.334155&saddr=60102","ddUrlFromHere":"http:\/\/www.google.com\/maps?source=uds&saddr=1611+South+Randall+Road,+Algonquin,+IL+(Brunswick+Zone+XL+Randall+Road)+@42.162958,-88.334155&iwstate1=dir:from","accuracy":"8","lat":"42.162958","viewportmode":"explicit"},{"region":"IL","streetAddress":"2075 East Algonquin Road","titleNoFormatting":"Brunswick Zone Algonquin","staticMapUrl":"http:\/\/maps.google.com\/maps\/api\/staticmap?maptype=roadmap&format=gif&sensor=false&size=150x100&zoom=13&markers=42.154629,-88.265871","listingType":"local","addressLines":["2075 East Algonquin Road","Algonquin, IL"],"lng":"-88.265871","phoneNumbers":[{"type":"","number":"(847) 658-9200"}],"url":"http:\/\/www.google.com\/maps\/place?source=uds&q=brunswick+zone&cid=7798335569608325784","country":"United States","city":"Algonquin","content":"","GsearchResultClass":"GlocalSearch","maxAge":604800,"title":"<b>Brunswick Zone<\/b> Algonquin","ddUrlToHere":"http:\/\/www.google.com\/maps?source=uds&daddr=2075+East+Algonquin+Road,+Algonquin,+IL+(Brunswick+Zone+Algonquin)+@42.154629,-88.265871&iwstate1=dir:to","ddUrl":"http:\/\/www.google.com\/maps?source=uds&daddr=2075+East+Algonquin+Road,+Algonquin,+IL+(Brunswick+Zone+Algonquin)+@42.154629,-88.265871&saddr=60102","ddUrlFromHere":"http:\/\/www.google.com\/maps?source=uds&saddr=2075+East+Algonquin+Road,+Algonquin,+IL+(Brunswick+Zone+Algonquin)+@42.154629,-88.265871&iwstate1=dir:from","accuracy":"8","lat":"42.154629","viewportmode":"explicit"}],"viewport":{"center":{"lng":"-88.48145","lat":"42.281384"},"sw":{"lng":"-88.74015","lat":"42.129276"},"ne":{"lng":"-88.222755","lat":"42.43349"},"span":{"lng":"0.51739","lat":"0.304211"}},"cursor":{"moreResultsUrl":"http:\/\/www.google.com\/local?oe=utf8&ie=utf8&num=4&mrt=yp,loc&sll=37.779160,-122.420090&start=0&hl=en&q=brunswick+zone+60102","currentPageIndex":0,"estimatedResultCount":"258","pages":[{"start":"0","label":1},{"start":"4","label":2},{"start":"8","label":3},{"start":"12","label":4}]}},"responseStatus":200,"responseDetails":null}
The outer-most (single) label is "responseData" The first (also single) nested label is "results" Within the "results" object, I have 2 identical netsed data sets, each representing a single complete google search result, which contain the elements, that I needs, such as: "titleNoFormatting", "addressLines" and "phoneNumbers".
I'm writing my first Android java app, and 开发者_StackOverflow中文版really struggling with extracting the values I need. I have looked into Gson and Jackson, but, was unable to conceive a solution for myself. I think, part of the issue may have to do with a fact, that these inner data sets do not have explicit container names, they just have identical structure: the outer label "results" is present only once and contains both identical data sets. Could anyone, please, provide an example of how to deal with this?
In Gson, the {}
in JSON can be mapped to Map<String, Object>
or a fullworthy Javabean. The []
in JSON can be mapped to a List<Object>
or an Object[]
.
With your current structure and Google Gson, I'd suggest the following:
public class GoogleResults {
private ResponseData responseData;
// Add/generate getter+setter.
static class ResponseData {
private List<Result> results;
// Add/generate getter+setter.
}
static class Result {
private String titleNoFormatting;
private List<String> addressLines;
private List<Map<String, String>> phoneNumbers; // Or List<PhoneNumber>
// Add/generate getters+setters.
}
}
Which you can use as follows:
GoogleResults results = new Gson().fromJson(json, GoogleResults.class);
You can use Android's JSONObject to create a JSON Object representation from a JSON String.
Eg
JSONObject json = new JSONObject("..."); //Where the string value is the JSON from your question.
JSONArray results = json.getJSONObject("responseData").getJSONArray("results);
And you can now iterate through results
via length()
.
精彩评论