慕工程0101907
2021-10-13 10:15:17
{ "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條經(jīng)驗 獲得超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條經(jīng)驗 獲得超4個贊
首先,您必須為您的 JSON 準備一個匹配的類結(jié)構。如果完成,在您的代碼中實例化一個 ObjectMapper 并從中獲取對象。
ObjectMapper mapper = new ObjectMapper();
YourType unmarshalledJson = mapper.readValue(jsonString,YourType.class);
添加回答
舉報
0/150
提交
取消