Java json simple与jackson在没有obj类型时解析json

Java json simple与jackson在没有obj类型时解析json,java,json,jackson,json-simple,Java,Json,Jackson,Json Simple,要使用jackson API将json字符串转换为pojo,可以使用: String jsonInString = "{\"age\":33,\"messages\":[\"msg 1\",\"msg 2\"],\"name\":\"mkyong\"}"; User user1 = mapper.readValue(jsonInString, User.class); 这需要创建和json字符串结构匹配的类用户 使用json简单API可以改为使用: JSONObject json = (JSO

要使用jackson API将json字符串转换为pojo,可以使用:

String jsonInString = "{\"age\":33,\"messages\":[\"msg 1\",\"msg 2\"],\"name\":\"mkyong\"}";
User user1 = mapper.readValue(jsonInString, User.class);
这需要创建和json字符串结构匹配的类用户

使用json简单API可以改为使用:

JSONObject json = (JSONObject)new JSONParser().parse(jsonInString);

使用json simple不需要包含与json格式匹配的pojo。类似的方法可以用在杰克逊身上吗?json simple不太冗长,因为不必创建与json结构匹配的类。

Jackson可以将json字符串反序列化为通用映射:

Map<String, Object> m  = new ObjectMapper().readValue(jsonInString, Map.class);
for (Map.Entry<String, Object> entry : m.entrySet()) {
    System.out.println(entry.getKey() + " -> " + entry.getValue() + "(" + entry.getValue().getClass().getName() + ")");
}

您可以使用类似的API

JsonNode node = mapper.readTree(jsonInString);
JsonNode node = mapper.readTree(jsonInString);