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

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

JSON是一种与JavaScript应用程序配合良好的数据格式,已成为REST数据服务的默认选择。在这个例子中,我们将展示如何使用GSONjackson这两个流行的开源库将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\"}";

GSON

JSON到对象

@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}

JSON到List

@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}
]

JSON到数组

@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}
]

Jackson

JSON到对象

@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}

JSON到List

@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}
]

JSON到数组

@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}
]

相关文章

最新文章

更多