0

我有以下 Json 字符串,来自 google 搜索查询:

{"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}

最外层(单个)标签是“responseData” 第一个(也是单个)嵌套标签是“results” 在“results”对象中,我有 2 个相同的网状数据集,每个代表一个完整的 google 搜索结果,其中包含我需要的元素,例如:“titleNoFormatting”、“addressLines”和“phoneNumbers”。

我正在编写我的第一个 Android java 应用程序,并且非常努力地提取我需要的值。我研究过 Gson 和 Jackson,但无法为自己想出解决方案。我认为,部分问题可能与一个事实有关,即这些内部数据集没有明确的容器名称,它们只是具有相同的结构:外部标签“结果”仅出现一次,并且包含两个相同的数据集。请任何人提供如何处理此问题的示例?

4

2 回答 2

2

在 Gson 中,{}可以将 in JSON 映射到Map<String, Object>一个完整的 Javabean。JSON 中的[]可以映射到 aList<Object>Object[].

使用您当前的结构和 Google Gson,我建议以下内容:

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.
    }
}

您可以按如下方式使用:

GoogleResults results = new Gson().fromJson(json, GoogleResults.class);
于 2010-11-29T16:00:58.597 回答
1

您可以使用 Android 的JSONObject从 JSON 字符串创建 JSON 对象表示。

例如

JSONObject json = new JSONObject("..."); //Where the string value is the JSON from your question.
JSONArray results = json.getJSONObject("responseData").getJSONArray("results);

你现在可以遍历resultsvia length()

于 2010-11-29T16:04:38.863 回答