{ "first":"element", "second":"Integral", "isThird":false, "fourth":{ "ONE":[ { "100":"Cars" }, { "200":"Truck" } ], "TWO":[ { "6":"Vintage" }, { "4":"Sports" } ] }}我有一个 json,我在其中使用 Jackson 以 Java 对象形式中断。我想知道如何使用 Jackson 将这个 json 分解成最简单的形式。这是我使用的杰克逊依赖<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.6.0</version></dependency>
2 回答
噜噜哒
TA贡献1784条经验 获得超7个赞
使用 Gson API。
JsonParser parser=new JsonParser();
JsonObject jsonObj=(JsonObject) parser.parse("Your Json String");
String first=jsonObj.get("first"); // first and second are String in json
String second=jsonObj.get("second");
JsonObject fourth=jsonObj.getAsJsonObject("fourth"); // because fourth is Object in json
// And so on......
九州编程
TA贡献1785条经验 获得超4个赞
首先,您必须为您的 JSON 准备一个匹配的类结构。如果完成,在您的代码中实例化一个 ObjectMapper 并从中获取对象。
ObjectMapper mapper = new ObjectMapper();
YourType unmarshalledJson = mapper.readValue(jsonString,YourType.class);
添加回答
举报
0/150
提交
取消