Json:如何使用java从单个外部Json对象中提取内部Json对象
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Gson 中,JSON 中的
{}
可以映射到Map
或完整的 Javabean。 JSON 中的[]
可以映射到List
根据您当前的结构和 Google Gson,我建议如下:
您可以按如下方式使用:
In Gson, the
{}
in JSON can be mapped toMap<String, Object>
or a fullworthy Javabean. The[]
in JSON can be mapped to aList<Object>
or anObject[]
.With your current structure and Google Gson, I'd suggest the following:
Which you can use as follows:
您可以使用 Android 的 JSONObject 从 JSON 字符串创建 JSON 对象表示。
例如
,您现在可以通过
length()
迭代结果
。You can use Android's JSONObject to create a JSON Object representation from a JSON String.
Eg
And you can now iterate through
results
vialength()
.