亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Jackson 解析深度嵌套的 JSON 屬性

使用 Jackson 解析深度嵌套的 JSON 屬性

翻過高山走不出你 2023-08-04 15:16:45
我正在嘗試找到一種干凈的方法來解析API.以下是有效負載的粗略概括JSON:{  "root": {    "data": {      "value": [        {          "user": {            "id": "1",            "name": {              "first": "x",              "last": "y"            }          }        }      ]    }  }}我的目標是擁有一組User具有firstName和lastName字段的對象。有誰知道一個干凈地解析這個的好方法嗎?現在我正在嘗試創建一個Wrapper類,并在其中包含用于數據、值、用戶等的靜態內部類,但這似乎是一種混亂的方式,只是為了讀取第一個/最后一個屬性的數組。我用來restTemplate.exchange()調用端點。
查看完整描述

2 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

JsonPath庫允許您僅選擇必需的字段,然后您可以使用它將Jackson原始數據轉換為POJO類。示例解決方案如下所示:

import com.fasterxml.jackson.annotation.JsonProperty;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.type.CollectionType;

import com.jayway.jsonpath.JsonPath;


import java.io.File;

import java.util.List;

import java.util.Map;


public class JsonPathApp {


? ? public static void main(String[] args) throws Exception {

? ? ? ? File jsonFile = new File("./resource/test.json").getAbsoluteFile();


? ? ? ? List<Map> nodes = JsonPath.parse(jsonFile).read("$..value[*].user.name");


? ? ? ? ObjectMapper mapper = new ObjectMapper();

? ? ? ? CollectionType usersType = mapper.getTypeFactory().constructCollectionType(List.class, User.class);

? ? ? ? List<User> users = mapper.convertValue(nodes, usersType);

? ? ? ? System.out.println(users);

? ? }

}


class User {


? ? @JsonProperty("first")

? ? private String firstName;


? ? @JsonProperty("last")

? ? private String lastName;


? ? public String getFirstName() {

? ? ? ? return firstName;

? ? }


? ? public void setFirstName(String firstName) {

? ? ? ? this.firstName = firstName;

? ? }


? ? public String getLastName() {

? ? ? ? return lastName;

? ? }


? ? public void setLastName(String lastName) {

? ? ? ? this.lastName = lastName;

? ? }


? ? @Override

? ? public String toString() {

? ? ? ? return "User{" +

? ? ? ? ? ? ? ? "firstName='" + firstName + '\'' +

? ? ? ? ? ? ? ? ", lastName='" + lastName + '\'' +

? ? ? ? ? ? ? ? '}';

? ? }

}

上面的代碼打?。?/p>


[User{firstName='x', lastName='y'}]


查看完整回答
反對 回復 2023-08-04
?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

另一種簡單的方法是使用JSON.simple庫:


JSONParser jsonParser = new JSONParser();

? ? ? ? //Read JSON file

? ? ? ? Object obj = jsonParser.parse(reader);


? ? ? ? JSONObject jObj = (JSONObject) obj;


? ? ? ? JSONObject root = (JSONObject)jObj.get("root");

? ? ? ? JSONObject data = (JSONObject) root.get("data");

? ? ? ? JSONArray value =? (JSONArray) data.get("value");

? ? ? ? JSONObject array = (JSONObject) value.get(0);

? ? ? ? JSONObject user = (JSONObject) array.get("user");

? ? ? ? JSONObject name = (JSONObject) user.get("name");


? ? ? ? String lastName = (String) name.get("last");

? ? ? ? String firstName = (String) name.get("first");


? ? ? ? System.out.println(lastName + " " + firstName);


查看完整回答
反對 回復 2023-08-04
  • 2 回答
  • 0 關注
  • 286 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號