如何从 RestTemplate 将 json 反序列化为单个对象?
How do I deserialize json into a single object from RestTemplate?
我必须使用 exchange() 方法,因为我在那里传递了 HttpHeaders。
ResponseEntity<WeatherResponse> response Entity = restTemplate.exchange(
weather UrlRequest, Http Method.GET, new HttpEntity<>(headers), WeatherResponse.class);
JSON:
{
"geoloc": {
"city": {
"id": 213,
"name": "Boston"
},
"country": {
"id": 213,
"name": "USA"
},
"temp": {
"value": 19.4
}
}
要反序列化的对象:
class WeahterResponse{
String country;
String city;
float temp;
}
在这种情况下如何影响反序列化。 JSON 中有两个对象,我需要一个?
class WeahterResponse{
GeoLocation geoloc;
Map<String,String> temp;
}
class GeoLocation {
Map<String,Map<String,Object> geoData;
}
It will deserialize your data to WeatherResponse.
Now if you want to get city data or country Data you can get that as follows.
suppose json is deserialized into weatherResponse.
Map<String,Map<String,Object> geoData = weatherResponse.getGeoLoc();
if(!CollectionUtils.isEmpty(geoData)){
if(geoData.containsKey("city")){
Map<String,Object> cityData = geoData.get("city");
System.out.println(cityData.get("id");
System.out.println(cityData.get("name");
}
//same for other keys of geoLoc
//to get Temp value
Map<String,String> temp = weatherResponse.getTemp();
System.out.println(temp.get("value");
感谢 João Dias。
我制作了自定义反序列化器。 https://www.baeldung.com/jackson-deserialization
就我而言:
public class WeatherDeserializer extends JsonDeserializer<WeatherResponse> {
@Override
public WeatherResponse deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode nodeTree = jsonParser.getCodec().readTree(jsonParser);
JsonNode geoObjectNode = nodeTree.get("geo_object");
JsonNode factNode = nodeTree.get("fact");
String country = geoObjectNode.get("country").get("name").textValue();
String province = geoObjectNode.get("province").get("name").textValue();
String locality = geoObjectNode.get("locality").get("name").textValue();
GeoObject geoObject = new GeoObject(country, province, locality);
Short temp = factNode.get("temp").shortValue();
Long obsTime = factNode.get("uptime").longValue();
return new WeatherResponse(geoObject,temp,obsTime);
}
}
@AllArgsConstructor
@Getter
@JsonDeserialize(using = WeatherDeserializer.class)
public class WeatherResponse {
private GeoObject geoObject;
private Short temp;
private Long uptime;
}
我必须使用 exchange() 方法,因为我在那里传递了 HttpHeaders。
ResponseEntity<WeatherResponse> response Entity = restTemplate.exchange(
weather UrlRequest, Http Method.GET, new HttpEntity<>(headers), WeatherResponse.class);
JSON:
{
"geoloc": {
"city": {
"id": 213,
"name": "Boston"
},
"country": {
"id": 213,
"name": "USA"
},
"temp": {
"value": 19.4
}
}
要反序列化的对象:
class WeahterResponse{
String country;
String city;
float temp;
}
在这种情况下如何影响反序列化。 JSON 中有两个对象,我需要一个?
class WeahterResponse{
GeoLocation geoloc;
Map<String,String> temp;
}
class GeoLocation {
Map<String,Map<String,Object> geoData;
}
It will deserialize your data to WeatherResponse.
Now if you want to get city data or country Data you can get that as follows.
suppose json is deserialized into weatherResponse.
Map<String,Map<String,Object> geoData = weatherResponse.getGeoLoc();
if(!CollectionUtils.isEmpty(geoData)){
if(geoData.containsKey("city")){
Map<String,Object> cityData = geoData.get("city");
System.out.println(cityData.get("id");
System.out.println(cityData.get("name");
}
//same for other keys of geoLoc
//to get Temp value
Map<String,String> temp = weatherResponse.getTemp();
System.out.println(temp.get("value");
感谢 João Dias。 我制作了自定义反序列化器。 https://www.baeldung.com/jackson-deserialization
就我而言:
public class WeatherDeserializer extends JsonDeserializer<WeatherResponse> {
@Override
public WeatherResponse deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode nodeTree = jsonParser.getCodec().readTree(jsonParser);
JsonNode geoObjectNode = nodeTree.get("geo_object");
JsonNode factNode = nodeTree.get("fact");
String country = geoObjectNode.get("country").get("name").textValue();
String province = geoObjectNode.get("province").get("name").textValue();
String locality = geoObjectNode.get("locality").get("name").textValue();
GeoObject geoObject = new GeoObject(country, province, locality);
Short temp = factNode.get("temp").shortValue();
Long obsTime = factNode.get("uptime").longValue();
return new WeatherResponse(geoObject,temp,obsTime);
}
}
@AllArgsConstructor
@Getter
@JsonDeserialize(using = WeatherDeserializer.class)
public class WeatherResponse {
private GeoObject geoObject;
private Short temp;
private Long uptime;
}