java將Object對象轉換成實體類對象


一般在接收前端傳過來的Json字符串時,需要將復雜的字符串通過轉換,變成相應的實體對象從而進行操作

1.通過類型強制轉換,將Json字符串中的內容轉換為對應的對象信息  

 String str ="{\"cells\":[{\"position\":{\"x\":870,\"y\":135},\"size\":{\"width\":1,\"height\":1},\"attrs\":{\"text\":{\"text\":\"母線6-150KV\"}},\"shape\":\"rect\",\"id\":\"4df7abde-19c3-41e0-a7b3-80917098acb5\",\"data\":{\"cellType\":\"label\",\"label\":1,\"content\":{\"value\":\"母線-name\"},\"partTwo\":[]},\"visible\":false,\"shape\":\"electric-component-base\",\"zIndex\":23,\"parent\":\"e3ed22a5-8cf4-45a9-835d-43ce4e47bf55\"},{\"position\":{\"x\":870,\"y\":170},\"size\":{\"width\":1,\"height\":1},\"attrs\":{\"text\":{\"text\":150}},\"shape\":\"rect\",\"id\":\"96e6ff54-db70-4c55-a09d-66b20b42acc7\",\"data\":{\"cellType\":\"label\",\"label\":2,\"content\":{\"value\":\"母線-basicVoltage\"},\"partTwo\":[[0,0,0,\"NULL\",0,0,0,0,0,0,1],[1,1,1,\"CSS\",1,1,1,1,1,1,0]]},\"visible\":false,\"shape\":\"electric-component-base\",\"zIndex\":24,\"parent\":\"e3ed22a5-8cf4-45a9-835d-43ce4e47bf55\"},{\"position\":{\"x\":-10,\"y\":405},\"size\":{\"width\":1,\"height\":1},\"attrs\":{\"text\":{\"text\":\"母線1-165KV\"}},\"shape\":\"electric-component-base\",\"id\":\"a2748f66-2e64-4548-9d59-df4e2e09a154\",\"data\":{\"label\":4,\"cellType\":\"label\",\"content\":{\"value\":\"母線-name\"},\"partTwo\":[[2,2,2,\"NULL\",2,2,2,2,2,2,1],[3,3,3,\"CSS\",3,3,3,3,3,3,1]]},\"visible\":true,\"zIndex\":3,\"parent\":\"13ff8214-6677-4b41-bde2-37ad63fe5838\"}]}";
        JSONObject jsonObject = JSONObject.parseObject(str);
        JSONArray jsonArray = null;
        jsonArray = jsonObject.getJSONArray("cells");
for (int i = 0; i < jsonArray.size(); i++) {
            ObjectMapper objectMapper = new ObjectMapper();
            PartTwo partTwo = (PartTwo)jsonArray.getJSONObject(i).get("data");//類型強制轉換
        }

但存在如果Object從其他地方獲取到后強轉為自定義對象時會報錯,並且強轉對象的話並不靈活

 

 

2.利用 com.fasterxml.jackson.databind.ObjectMapper 包下的 convertValue方法可將對象轉換為對應的實體類對象

引入jackson.databind

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.3</version>
        </dependency>

 

convertValue(Object fromValue, Class<T> toValueType)

通過convertValue將Object對象轉換為相應實體,前提是fromValue數據即為對應的class對象

 String str ="{\"cells\":[{\"position\":{\"x\":870,\"y\":135},\"size\":{\"width\":1,\"height\":1},\"attrs\":{\"text\":{\"text\":\"母線6-150KV\"}},\"shape\":\"rect\",\"id\":\"4df7abde-19c3-41e0-a7b3-80917098acb5\",\"data\":{\"cellType\":\"label\",\"label\":1,\"content\":{\"value\":\"母線-name\"},\"partTwo\":[]},\"visible\":false,\"shape\":\"electric-component-base\",\"zIndex\":23,\"parent\":\"e3ed22a5-8cf4-45a9-835d-43ce4e47bf55\"},{\"position\":{\"x\":870,\"y\":170},\"size\":{\"width\":1,\"height\":1},\"attrs\":{\"text\":{\"text\":150}},\"shape\":\"rect\",\"id\":\"96e6ff54-db70-4c55-a09d-66b20b42acc7\",\"data\":{\"cellType\":\"label\",\"label\":2,\"content\":{\"value\":\"母線-basicVoltage\"},\"partTwo\":[[0,0,0,\"NULL\",0,0,0,0,0,0,1],[1,1,1,\"CSS\",1,1,1,1,1,1,0]]},\"visible\":false,\"shape\":\"electric-component-base\",\"zIndex\":24,\"parent\":\"e3ed22a5-8cf4-45a9-835d-43ce4e47bf55\"},{\"position\":{\"x\":-10,\"y\":405},\"size\":{\"width\":1,\"height\":1},\"attrs\":{\"text\":{\"text\":\"母線1-165KV\"}},\"shape\":\"electric-component-base\",\"id\":\"a2748f66-2e64-4548-9d59-df4e2e09a154\",\"data\":{\"label\":4,\"cellType\":\"label\",\"content\":{\"value\":\"母線-name\"},\"partTwo\":[[2,2,2,\"NULL\",2,2,2,2,2,2,1],[3,3,3,\"CSS\",3,3,3,3,3,3,1]]},\"visible\":true,\"zIndex\":3,\"parent\":\"13ff8214-6677-4b41-bde2-37ad63fe5838\"}]}";
        JSONObject jsonObject = JSONObject.parseObject(str);
        JSONArray jsonArray = null;
        jsonArray = jsonObject.getJSONArray("cells");
for (int i = 0; i < jsonArray.size(); i++) {
            ObjectMapper objectMapper = new ObjectMapper();
            PartTwo partTwo = objectMapper.convertValue(jsonArray.getJSONObject(i).get("data"),PartTwo.class);//通過convertValue方法將object對象轉換為相應實體對象
        }

 

 

上面轉換的過程中,如果返回的字段你不是都需要,需要忽略其中的幾個字段,在自定義的類中添加如下:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class PartTwo{
//  private Integer cellType; //Json字符串中有該參數,實體類中並未添加
  private String label;
}

 

或使用自定義設置忽略不想獲取的參數

ObjectMapper objectMapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM