{ "code": "0", "msg": "success", "data": { "total": 1, "list": [ { "personId": "3377634", "personName": "杨博", "gender": 1, "orgIndexCode": "13211331", "certificateType": 111, "certificateNo": "642223199811280016", "jobNo": "90223505", "birthday": "1998-11-28", "phoneNo": "18309540994", "email": "", "pinyin": "yangbo", "createTime": "2022-09-16T02:03:01.622+08:00", "updateTime": "2022-11-14T19:10:59.071+08:00", "faceNum": 1, "fingerprintNum": 0, "orgName": "宁夏宝丰能源集团股份有限公司信息化部", "orgPath": "@root000000@a1d7bcc3-7e73-4aa5-be78-e7448aed6111@13201129@13211280@13211331@", "orgPathName": "宝丰集团/员工同步组织/宁夏宝丰集团/宁夏宝丰能源集团股份有限公司/宁夏宝丰能源集团股份有限公司信息化部", "orgList": [ "13211331" ], "personPhoto": [ { "serverIndexCode": "b0c6e000-091c-4144-a95d-cbea801812d2", "personPhotoIndexCode": "0f5fb936-3172-4a11-82ec-51356ff0be30", "picUri": "/pic?0df926994e9do-bel*41-e05*8o0=6=4705*9l5944279166*7t==312**ps8=1b3=07c2*135e71327-a5308d-2pi1e4o=00c50=805" } ] } ] } }用java如何获取到personPhoto里面的字段
时间: 2023-11-30 09:03:46 浏览: 78
ASP.NET―001:GridView绑定List、页面返回值具体实现
可以使用 JSON 解析库,如 Jackson 或者 Gson,将 JSON 字符串解析成 Java 对象,然后通过对象的相应字段获取到 personPhoto 字段。假设使用 Jackson 库,代码如下:
```java
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
String jsonStr = "{ \"code\": \"0\", \"msg\": \"success\", \"data\": { \"total\": 1, \"list\": [ { \"personId\": \"3377634\", \"personName\": \"杨博\", \"gender\": 1, \"orgIndexCode\": \"13211331\", \"certificateType\": 111, \"certificateNo\": \"642223199811280016\", \"jobNo\": \"90223505\", \"birthday\": \"1998-11-28\", \"phoneNo\": \"18309540994\", \"email\": \"\", \"pinyin\": \"yangbo\", \"createTime\": \"2022-09-16T02:03:01.622+08:00\", \"updateTime\": \"2022-11-14T19:10:59.071+08:00\", \"faceNum\": 1, \"fingerprintNum\": 0, \"orgName\": \"宁夏宝丰能源集团股份有限公司信息化部\", \"orgPath\": \"@root000000@a1d7bcc3-7e73-4aa5-be78-e7448aed6111@13201129@13211280@13211331@\", \"orgPathName\": \"宝丰集团/员工同步组织/宁夏宝丰集团/宁夏宝丰能源集团股份有限公司/宁夏宝丰能源集团股份有限公司信息化部\", \"orgList\": [ \"13211331\" ], \"personPhoto\": [ { \"serverIndexCode\": \"b0c6e000-091c-4144-a95d-cbea801812d2\", \"personPhotoIndexCode\": \"0f5fb936-3172-4a11-82ec-51356ff0be30\", \"picUri\": \"/pic?0df926994e9do-bel*41-e05*8o0=6=4705*9l5944279166*7t==312**ps8=1b3=07c2*135e71327-a5308d-2pi1e4o=00c50=805\" } ] } ] } }";
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonStr);
JsonNode personPhoto = jsonNode.at("/data/list/0/personPhoto");
System.out.println(personPhoto);
```
其中,`jsonStr` 是待解析的 JSON 字符串,`JsonNode` 是 Jackson 库中的 JSON 对象,`objectMapper` 是 Jackson 的解析器,`at` 方法是用来获取指定 JSON 节点的方法。运行上面的代码,会输出 `personPhoto` 字段的值,即:
```json
[ {
"serverIndexCode" : "b0c6e000-091c-4144-a95d-cbea801812d2",
"personPhotoIndexCode" : "0f5fb936-3172-4a11-82ec-51356ff0be30",
"picUri" : "/pic?0df926994e9do-bel*41-e05*8o0=6=4705*9l5944279166*7t==312**ps8=1b3=07c2*135e71327-a5308d-2pi1e4o=00c50=805"
} ]
```
阅读全文