如何从 Map<String, Object> 解析嵌套的 JSON
How to parse nested JSON from Map<String, Object>
我正在使用下面的方法将 json 响应映射到 Map
Map<String, Object> apiResponse = restTemplate.postForObject("https://maps.googleapis.com/maps/api/geocode/json?address="+defaultLocation+"&key="+API_KEY, httpEntity, Map.class, Collections.EMPTY_MAP);
我可以使用下面的代码将整个 JSON 输出到一个字符串
String jsonResponse = apiResponse.get("results").toString();
然而,我想要得到的是一个嵌套值,即 results->geometry->location
我尝试了一些 JSONArrays, JSONObjects, Substring
的解决方案,但无法使它们起作用。
回复JSON:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "New Zealand",
"short_name" : "NZ",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Auckland, New Zealand",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : -36.660571,
"lng" : 175.287137
},
"southwest" : {
"lat" : -37.065475,
"lng" : 174.4438016
}
},
"location" : {
"lat" : -36.8484597,
"lng" : 174.7633315
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : -36.660571,
"lng" : 175.287137
},
"southwest" : {
"lat" : -37.065475,
"lng" : 174.4438016
}
}
},
"place_id" : "ChIJ--acWvtHDW0RF5miQ2HvAAU",
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "New Zealand",
"short_name" : "NZ",
"types" : [ "country", "political" ]
},
{
"long_name" : "1010",
"short_name" : "1010",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Auckland, 1010, New Zealand",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : -36.8364659,
"lng" : 174.7838398
},
"southwest" : {
"lat" : -36.8621041,
"lng" : 174.7503805
}
},
"location" : {
"lat" : -36.8484597,
"lng" : 174.7633315
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : -36.8364659,
"lng" : 174.7838398
},
"southwest" : {
"lat" : -36.8621041,
"lng" : 174.7503805
}
}
},
"place_id" : "ChIJuZqpSPtHDW0R4LOiQ2HvAAU",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
}
],
"status" : "OK"
}
如有任何帮助,我们将不胜感激。
理想情况下,您希望使用与在 JS 中相同的本机符号来访问属性。像这样:
String url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + address;
String responseStr = fetch(url);
JsonHelper response = JsonHelper.forString(responseStr);
String status = (String) response.getValue("status");
if(status != null && status.equals("OK")) {
lat = (Double) response.getValue("results[0].geometry.location.lat");
lng = (Double) response.getValue("results[0].geometry.location.lng");
}
下面的 JsonHelper
class 代码(取自 jello-framework)可以让你做到这一点。
package jello.common;
import java.util.List;
import com.google.gson.Gson;
import java.util.AbstractMap;
public class JsonHelper {
private Object json;
public JsonHelper(String jsonString) {
Gson g = new Gson();
json = g.fromJson(jsonString, Object.class);
}
public static JsonHelper forString(String jsonString) {
return new JsonHelper(jsonString);
}
@SuppressWarnings("unchecked")
public Object getValue(String path) {
Object value = json;
String [] elements = path.split("\.");
for(String element : elements) {
String ename = element.split("\[")[0];
if(AbstractMap.class.isAssignableFrom(value.getClass())) {
value = ( (AbstractMap<String, Object>) value).get(ename);
if(element.contains("[")) {
if(List.class.isAssignableFrom(value.getClass())) {
Integer index = Integer.valueOf(element.substring(element.indexOf("[")+1, element.indexOf("]")) );
value = ((List<Object>) value).get(index);
}
else {
return null;
}
}
}
else {
return null;
}
}
return value;
}
}
JSONObject obj=new JSONObject(jsonresult);
// get result array
JSONArray resultsarray= obj.getJSONArray("results");
for (int i=0;i<resultsarray.length(),i++){
// get Objects using index
JSONObject jsonobject= results.getJSONObject(i);
// get geometry object
JSONObject geometry= jsonobject.getJSONObject("geometry");
// get location object from geometry
JSONObject location= geometry.getJSONObject("location");
// get location values from location object
double lat = location.optDouble("lat",0.0);
double long = location.optDouble("lng",0.0);
}
关于optDouble
public double optDouble(String key, double defaultValue) {
Get an optional double associated with a key, or the defaultValue if
there is no such key or if its value is not a number. If the value is
a string, an attempt will be made to evaluate it as a number.
使用jackson api解析,会很简单
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);
if(node.get("results").isArray()){
for(int i=0; i <= node.get("results").size()-1; i++){
System.out.println(node.get("results").get(i));
}
我使用了 Gson api 并且能够获取位置。试试这个:
代码::
Gson gson = new Gson();
String json = "your json";
JsonObject map = gson.fromJson(json, JsonObject.class); // to be replaced with your restTemplate call
JsonArray arr = map.getAsJsonArray("results");
for (Object j : arr) {
System.out.println(((JsonObject) j).get("geometry").getAsJsonObject().get("location"));
}
控制台输出::
{"lat":-36.8484597,"lng":174.7633315}
{"lat":-36.8484597,"lng":174.7633315}
因此,理想情况下,只需将响应作为 JsonObject
而不是 Map
,您就可以阅读 location
.
我正在使用下面的方法将 json 响应映射到 Map
Map<String, Object> apiResponse = restTemplate.postForObject("https://maps.googleapis.com/maps/api/geocode/json?address="+defaultLocation+"&key="+API_KEY, httpEntity, Map.class, Collections.EMPTY_MAP);
我可以使用下面的代码将整个 JSON 输出到一个字符串
String jsonResponse = apiResponse.get("results").toString();
然而,我想要得到的是一个嵌套值,即 results->geometry->location
我尝试了一些 JSONArrays, JSONObjects, Substring
的解决方案,但无法使它们起作用。
回复JSON:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "New Zealand",
"short_name" : "NZ",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Auckland, New Zealand",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : -36.660571,
"lng" : 175.287137
},
"southwest" : {
"lat" : -37.065475,
"lng" : 174.4438016
}
},
"location" : {
"lat" : -36.8484597,
"lng" : 174.7633315
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : -36.660571,
"lng" : 175.287137
},
"southwest" : {
"lat" : -37.065475,
"lng" : 174.4438016
}
}
},
"place_id" : "ChIJ--acWvtHDW0RF5miQ2HvAAU",
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Auckland",
"short_name" : "Auckland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "New Zealand",
"short_name" : "NZ",
"types" : [ "country", "political" ]
},
{
"long_name" : "1010",
"short_name" : "1010",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Auckland, 1010, New Zealand",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : -36.8364659,
"lng" : 174.7838398
},
"southwest" : {
"lat" : -36.8621041,
"lng" : 174.7503805
}
},
"location" : {
"lat" : -36.8484597,
"lng" : 174.7633315
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : -36.8364659,
"lng" : 174.7838398
},
"southwest" : {
"lat" : -36.8621041,
"lng" : 174.7503805
}
}
},
"place_id" : "ChIJuZqpSPtHDW0R4LOiQ2HvAAU",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
}
],
"status" : "OK"
}
如有任何帮助,我们将不胜感激。
理想情况下,您希望使用与在 JS 中相同的本机符号来访问属性。像这样:
String url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + address;
String responseStr = fetch(url);
JsonHelper response = JsonHelper.forString(responseStr);
String status = (String) response.getValue("status");
if(status != null && status.equals("OK")) {
lat = (Double) response.getValue("results[0].geometry.location.lat");
lng = (Double) response.getValue("results[0].geometry.location.lng");
}
下面的 JsonHelper
class 代码(取自 jello-framework)可以让你做到这一点。
package jello.common;
import java.util.List;
import com.google.gson.Gson;
import java.util.AbstractMap;
public class JsonHelper {
private Object json;
public JsonHelper(String jsonString) {
Gson g = new Gson();
json = g.fromJson(jsonString, Object.class);
}
public static JsonHelper forString(String jsonString) {
return new JsonHelper(jsonString);
}
@SuppressWarnings("unchecked")
public Object getValue(String path) {
Object value = json;
String [] elements = path.split("\.");
for(String element : elements) {
String ename = element.split("\[")[0];
if(AbstractMap.class.isAssignableFrom(value.getClass())) {
value = ( (AbstractMap<String, Object>) value).get(ename);
if(element.contains("[")) {
if(List.class.isAssignableFrom(value.getClass())) {
Integer index = Integer.valueOf(element.substring(element.indexOf("[")+1, element.indexOf("]")) );
value = ((List<Object>) value).get(index);
}
else {
return null;
}
}
}
else {
return null;
}
}
return value;
}
}
JSONObject obj=new JSONObject(jsonresult);
// get result array
JSONArray resultsarray= obj.getJSONArray("results");
for (int i=0;i<resultsarray.length(),i++){
// get Objects using index
JSONObject jsonobject= results.getJSONObject(i);
// get geometry object
JSONObject geometry= jsonobject.getJSONObject("geometry");
// get location object from geometry
JSONObject location= geometry.getJSONObject("location");
// get location values from location object
double lat = location.optDouble("lat",0.0);
double long = location.optDouble("lng",0.0);
}
关于optDouble
public double optDouble(String key, double defaultValue) {
Get an optional double associated with a key, or the defaultValue if there is no such key or if its value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.
使用jackson api解析,会很简单
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);
if(node.get("results").isArray()){
for(int i=0; i <= node.get("results").size()-1; i++){
System.out.println(node.get("results").get(i));
}
我使用了 Gson api 并且能够获取位置。试试这个:
代码::
Gson gson = new Gson();
String json = "your json";
JsonObject map = gson.fromJson(json, JsonObject.class); // to be replaced with your restTemplate call
JsonArray arr = map.getAsJsonArray("results");
for (Object j : arr) {
System.out.println(((JsonObject) j).get("geometry").getAsJsonObject().get("location"));
}
控制台输出::
{"lat":-36.8484597,"lng":174.7633315}
{"lat":-36.8484597,"lng":174.7633315}
因此,理想情况下,只需将响应作为 JsonObject
而不是 Map
,您就可以阅读 location
.