我对REST的整个概念不熟悉并将json映射到Java对象。我知道我需要创建一个对象映射器用来创建Java对象的类文件。下面的json是使用resttemplate从WeatherUnderground返回的高端。对我来说,它似乎是一个对象数组,我只对current_observation和display_location对象中的那一刻感兴趣。它似乎也有几个嵌入其他物体的物体。
我认为我可以弄清楚的数组,但任何人都可以给我一些关于如何映射json对象的指示。
谢谢, 罗布
{
"response": {
"version": "0.1",
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"conditions": 1
}
},
"current_observation": {
"image": {
"url": "http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png",
"title": "Weather Underground",
"link": "http://www.wunderground.com"
},
"display_location": {
"full": "San Francisco, CA",
"city": "San Francisco",
"state": "CA",
"state_name": "California",
"country": "US",
"country_iso3166": "US",
"zip": "94101",
"latitude": "37.77500916",
"longitude": "-122.41825867",
"elevation": "47.00000000"
},
答案 0 :(得分:0)
你会发现它实际上非常简单,我建议Jackson 2,它有很多很好的文档,并且被广泛使用。
作为您的基本用法示例,我创建了一个Java POJO来将您的响应示例映射到它。
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Map;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Response {
private Map<String, Object> response;
@JsonProperty("current_observation")
private Map<String, Object> currentObservation;
public Map<String, Object> getResponse() {
return response;
}
public void setResponse(Map<String, Object> response) {
this.response = response;
}
public Map<String, Object> getCurrentObservation() {
return currentObservation;
}
public void setCurrentObservation(Map<String, Object> currentObservation) {
this.currentObservation = currentObservation;
}
}
然后使用Test类测试您的响应并将其绑定到POJO。
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
String responseString = "{\n" +
"\"response\": {\n" +
"\"version\": \"0.1\",\n" +
"\"termsofService\": \"http://www.wunderground.com/weather/api/d/terms.html\",\n" +
"\"features\": {\n" +
"\"conditions\": 1\n" +
"}\n" +
"},\n" +
"\"current_observation\": {\n" +
"\"image\": {\n" +
"\"url\": \"http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png\",\n" +
"\"title\": \"Weather Underground\",\n" +
"\"link\": \"http://www.wunderground.com\"\n" +
"},\n" +
"\"display_location\": {\n" +
"\"full\": \"San Francisco, CA\",\n" +
"\"city\": \"San Francisco\",\n" +
"\"state\": \"CA\",\n" +
"\"state_name\": \"California\",\n" +
"\"country\": \"US\",\n" +
"\"country_iso3166\": \"US\",\n" +
"\"zip\": \"94101\",\n" +
"\"latitude\": \"37.77500916\",\n" +
"\"longitude\": \"-122.41825867\",\n" +
"\"elevation\": \"47.00000000\"\n" +
"}\n" +
"}\n" +
"}";
try {
Response response = objectMapper.readValue(responseString, Response.class);
System.out.print("Output: "+ response.getResponse().get("termsofService"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
从样本响应打印termsOfService的main方法的输出:
Output: http://www.wunderground.com/weather/api/d/terms.html
希望它有所帮助,
JoséLuis
答案 1 :(得分:0)
我认为您要将Json
解码为Object
。许多Json
库都支持此功能。
以FastJson为例:
假设您有类 HelloObject :
// in HelloObject.java
public class HelloObject
{
private DisplayLocation display_location;
// and other members
// setter and getter
// ....
}
// in DisplayLocation.java
public class DisplayLocation{
private String full;
private String city;
// and other members
// setter and getter
// ...
}
// main
String jsonString = ...;
HelloObject object = JSON.parseObject(jsonString, HelloObject.class);
顺便说一下,FastJson是JSON
中速度最快的Java
库,远远优于gson
。
答案 2 :(得分:0)
JSON具有内置数据类型 - 字符串,布尔值,对象等 - 因此您只需要根据JSON的规范数据模型来遵循这些类型。例如,您有以下类:
加上我们可以调用ResponseWrapper的整个事物的包装器对象。
在Java中,您需要创建单独的类,因此Image将类似于
onClick
属性的名称应与JSON的名称相匹配。但是,如果要使用不同的东西,可以使用注释指定。例如,您可能希望public class Image {
private String url;
private String title;
private String link;
// getter and setters
}
代替displayLocation
,所以如果您使用杰克逊,那么
display_location
在更高级别,然后聚合这些 - 例如,ResponseWrapper将包含以下
@JsonProperty("display_location")
private String displayLocation;