JSON是一种与JavaScript应用程序配合良好的数据格式,已成为REST数据服务的默认选择。在这个例子中,我们将展示如何使用GSON和jackson这两个流行的开源库将json转换为java对象。此外,我们将展示如何将json转换为arraylist和array。
在setup方法中,我们将创建一个名为Plane的java对象,并创建常量
static class Plane {
private String planeType;
public Plane() {
}
public String getPlaneType() {
return planeType;
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("plane type: ", planeType).toString();
}
}
private final String ARRAY_AS_JSON = "[{\"planeType\":\"Commercial Transport Planes\"},{\"planeType\":\"General Aviation Planes\"},{\"planeType\":\"Military Planes\"},{\"planeType\":\"Sea Planes\"},{\"planeType\":\"Special Purpose Planes\"}]";
private static final String JSON_OBJECT = "{\"planeType\":\"Sea Planes\"}";
@Test
public void unmarshall_json_to_object_with_gson () {
Gson gson = new Gson();
Plane plane = gson.fromJson(JSON_OBJECT, Plane.class);
logger.info(plane);
assertEquals("Sea Planes", plane.getPlaneType());
}
输出
Plane{plane type: =Sea Planes}
@Test
public void unmarshall_json_to_list_with_gson () {
Gson gson = new Gson();
@SuppressWarnings("serial")
List<Plane> planes = gson.fromJson(ARRAY_AS_JSON, new TypeToken<List<Plane>>(){}.getType());
logger.info(planes);
assertEquals(5, planes.size());
}
输出
[
Plane{plane type: =Commercial Transport Planes},
Plane{plane type: =General Aviation Planes},
Plane{plane type: =Military Planes},
Plane{plane type: =Sea Planes},
Plane{plane type: =Special Purpose Planes}
]
@Test
public void unmarshall_json_to_java_array_with_gson () {
Gson gson = new Gson();
Plane[] planes = gson.fromJson(ARRAY_AS_JSON, Plane[].class);
logger.info(planes);
assertEquals(5, planes.length);
}
输出
[
Plane{plane type: =Commercial Transport Planes},
Plane{plane type: =General Aviation Planes},
Plane{plane type: =Military Planes},
Plane{plane type: =Sea Planes},
Plane{plane type: =Special Purpose Planes}
]
@Test
public void unmarshall_json_to_object_with_jackson () throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
Plane plane = objectMapper.readValue(JSON_OBJECT, Plane.class);
logger.info(plane);
assertEquals("Sea Planes", plane.getPlaneType());
}
输出
Plane{plane type: =Sea Planes}
@Test
public void unmarshall_json_to_list_with_jackson () throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
List<Plane> planes = objectMapper.readValue(ARRAY_AS_JSON,
objectMapper.getTypeFactory().constructCollectionType(List.class, Plane.class));
logger.info(planes);
assertEquals(5, planes.size());
}
输出
[
Plane{plane type: =Commercial Transport Planes},
Plane{plane type: =General Aviation Planes},
Plane{plane type: =Military Planes},
Plane{plane type: =Sea Planes},
Plane{plane type: =Special Purpose Planes}
]
@Test
public void unmarshall_json_to_java_array_with_jackson () throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
Plane[] planes = objectMapper.readValue(ARRAY_AS_JSON, JsontoJavaObject.Plane[].class);
logger.info(Arrays.toString(planes));
assertEquals(5, planes.length);
}
输出
[
Plane{plane type: =Commercial Transport Planes},
Plane{plane type: =General Aviation Planes},
Plane{plane type: =Military Planes},
Plane{plane type: =Sea Planes},
Plane{plane type: =Special Purpose Planes}
]
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : http://www.leveluplunch.com/java/examples/java-object-to-json/
内容来源于网络,如有侵权,请联系作者删除!