使用GSON和Jackson将JSON转换为Java对象

x33g5p2x  于2022-10-25 转载在 Java  
字(3.3k)|赞(0)|评价(0)|浏览(1014)

JSON是一种与JavaScript应用程序配合良好的数据格式,已成为REST数据服务的默认选择。在这个例子中,我们将展示如何使用GSONjackson这两个流行的开源库将json转换为java对象。此外,我们将展示如何将json转换为arraylist和array。
在setup方法中,我们将创建一个名为Plane的java对象,并创建常量

设置

  1. static class Plane {
  2. private String planeType;
  3. public Plane() {
  4. }
  5. public String getPlaneType() {
  6. return planeType;
  7. }
  8. @Override
  9. public String toString() {
  10. return Objects.toStringHelper(this).add("plane type: ", planeType).toString();
  11. }
  12. }
  13. private final String ARRAY_AS_JSON = "[{\"planeType\":\"Commercial Transport Planes\"},{\"planeType\":\"General Aviation Planes\"},{\"planeType\":\"Military Planes\"},{\"planeType\":\"Sea Planes\"},{\"planeType\":\"Special Purpose Planes\"}]";
  14. private static final String JSON_OBJECT = "{\"planeType\":\"Sea Planes\"}";

GSON

JSON到对象

  1. @Test
  2. public void unmarshall_json_to_object_with_gson () {
  3. Gson gson = new Gson();
  4. Plane plane = gson.fromJson(JSON_OBJECT, Plane.class);
  5. logger.info(plane);
  6. assertEquals("Sea Planes", plane.getPlaneType());
  7. }

输出

  1. Plane{plane type: =Sea Planes}

JSON到List

  1. @Test
  2. public void unmarshall_json_to_list_with_gson () {
  3. Gson gson = new Gson();
  4. @SuppressWarnings("serial")
  5. List<Plane> planes = gson.fromJson(ARRAY_AS_JSON, new TypeToken<List<Plane>>(){}.getType());
  6. logger.info(planes);
  7. assertEquals(5, planes.size());
  8. }

输出

  1. [
  2. Plane{plane type: =Commercial Transport Planes},
  3. Plane{plane type: =General Aviation Planes},
  4. Plane{plane type: =Military Planes},
  5. Plane{plane type: =Sea Planes},
  6. Plane{plane type: =Special Purpose Planes}
  7. ]

JSON到数组

  1. @Test
  2. public void unmarshall_json_to_java_array_with_gson () {
  3. Gson gson = new Gson();
  4. Plane[] planes = gson.fromJson(ARRAY_AS_JSON, Plane[].class);
  5. logger.info(planes);
  6. assertEquals(5, planes.length);
  7. }

输出

  1. [
  2. Plane{plane type: =Commercial Transport Planes},
  3. Plane{plane type: =General Aviation Planes},
  4. Plane{plane type: =Military Planes},
  5. Plane{plane type: =Sea Planes},
  6. Plane{plane type: =Special Purpose Planes}
  7. ]

Jackson

JSON到对象

  1. @Test
  2. public void unmarshall_json_to_object_with_jackson () throws JsonParseException, JsonMappingException, IOException {
  3. ObjectMapper objectMapper = new ObjectMapper();
  4. Plane plane = objectMapper.readValue(JSON_OBJECT, Plane.class);
  5. logger.info(plane);
  6. assertEquals("Sea Planes", plane.getPlaneType());
  7. }

输出

  1. Plane{plane type: =Sea Planes}

JSON到List

  1. @Test
  2. public void unmarshall_json_to_list_with_jackson () throws JsonParseException, JsonMappingException, IOException {
  3. ObjectMapper objectMapper = new ObjectMapper();
  4. List<Plane> planes = objectMapper.readValue(ARRAY_AS_JSON,
  5. objectMapper.getTypeFactory().constructCollectionType(List.class, Plane.class));
  6. logger.info(planes);
  7. assertEquals(5, planes.size());
  8. }

输出

  1. [
  2. Plane{plane type: =Commercial Transport Planes},
  3. Plane{plane type: =General Aviation Planes},
  4. Plane{plane type: =Military Planes},
  5. Plane{plane type: =Sea Planes},
  6. Plane{plane type: =Special Purpose Planes}
  7. ]

JSON到数组

  1. @Test
  2. public void unmarshall_json_to_java_array_with_jackson () throws JsonParseException, JsonMappingException, IOException {
  3. ObjectMapper objectMapper = new ObjectMapper();
  4. Plane[] planes = objectMapper.readValue(ARRAY_AS_JSON, JsontoJavaObject.Plane[].class);
  5. logger.info(Arrays.toString(planes));
  6. assertEquals(5, planes.length);
  7. }

输出

  1. [
  2. Plane{plane type: =Commercial Transport Planes},
  3. Plane{plane type: =General Aviation Planes},
  4. Plane{plane type: =Military Planes},
  5. Plane{plane type: =Sea Planes},
  6. Plane{plane type: =Special Purpose Planes}
  7. ]

相关文章

最新文章

更多