一般在接收前端传过来的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);