Json:如何使用java从单个外部Json对象中提取内部Json对象

发布于 2024-10-05 12:56:54 字数 4293 浏览 3 评论 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)[email protected],-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)[email protected],-88.334155&saddr=60102","ddUrlFromHere":"http:\/\/www.google.com\/maps?source=uds&saddr=1611+South+Randall+Road,+Algonquin,+IL+(Brunswick+Zone+XL+Randall+Road)[email protected],-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)[email protected],-88.265871&iwstate1=dir:to","ddUrl":"http:\/\/www.google.com\/maps?source=uds&daddr=2075+East+Algonquin+Road,+Algonquin,+IL+(Brunswick+Zone+Algonquin)[email protected],-88.265871&saddr=60102","ddUrlFromHere":"http:\/\/www.google.com\/maps?source=uds&saddr=2075+East+Algonquin+Road,+Algonquin,+IL+(Brunswick+Zone+Algonquin)[email protected],-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” 第一个(也是单个)嵌套标签是“结果” 在“结果”对象中,我有两个相同的网络数据集,每个数据集代表一个完整的谷歌搜索结果,其中包含我需要的元素,例如: “titleNoFormatting”、“addressLines”和“phoneNumbers”。

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

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)[email protected],-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)[email protected],-88.334155&saddr=60102","ddUrlFromHere":"http:\/\/www.google.com\/maps?source=uds&saddr=1611+South+Randall+Road,+Algonquin,+IL+(Brunswick+Zone+XL+Randall+Road)[email protected],-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)[email protected],-88.265871&iwstate1=dir:to","ddUrl":"http:\/\/www.google.com\/maps?source=uds&daddr=2075+East+Algonquin+Road,+Algonquin,+IL+(Brunswick+Zone+Algonquin)[email protected],-88.265871&saddr=60102","ddUrlFromHere":"http:\/\/www.google.com\/maps?source=uds&saddr=2075+East+Algonquin+Road,+Algonquin,+IL+(Brunswick+Zone+Algonquin)[email protected],-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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不吻晚风 2024-10-12 12:56:54

在 Gson 中,JSON 中的 {} 可以映射到 Map 或完整的 Javabean。 JSON 中的 [] 可以映射到 ListObject[]

根据您当前的结构和 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);

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);
雨后彩虹 2024-10-12 12:56:54

您可以使用 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);

,您现在可以通过length() 迭代结果

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文