gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:48:14 24 4
gpt4 key购买 nike

我有以下 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”第一个(也是单个)嵌套标签是“结果”在“结果”对象中,我有两个相同的网络数据集,每个数据集代表一个完整的谷歌搜索结果,其中包含我需要的元素,例如:“titleNoFormatting”、“addressLines”和“phoneNumbers”。

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

最佳答案

在 Gson 中,{} JSON 中的内容可以映射到 Map<String, Object>或一个完全值得的Javabean。 [] JSON 中的格式可以映射到 List<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);

关于java - Json:如何使用java从单个外部Json对象中提取内部Json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4305342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com