用java实现将以下报文中的{ "code": "0", "msg": "SUCCESS", "data": { "total": 1, "list": [ { "personId": "f03fdef8356548c88c36fb8e50b73db7", "personPhoto": { "personPhotoIndexCode": "7114be-c23a-49a2-96bf-52e2f12311d", "personId": "8ffb3b91-f823-42b6-8fca-137bff553857" }, "email": "null", "education": 7, "lastName": "王", "givenName": "五", "age": 30 } ] } }personPhotoIndexCode字段取出
时间: 2023-07-19 10:43:24 浏览: 488
可以使用Java中的json解析库,比如Jackson、Gson等。以下是使用Jackson解析的代码示例:
```java
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public static void main(String[] args) throws Exception {
String jsonStr = "{\"code\":\"0\",\"msg\":\"SUCCESS\",\"data\":{\"total\":1,\"list\":[{\"personId\":\"f03fdef8356548c88c36fb8e50b73db7\",\"personPhoto\":{\"personPhotoIndexCode\":\"7114be-c23a-49a2-96bf-52e2f12311d\",\"personId\":\"8ffb3b91-f823-42b6-8fca-137bff553857\"},\"email\":\"null\",\"education\":7,\"lastName\":\"王\",\"givenName\":\"五\",\"age\":30}]}}";
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(jsonStr);
String personPhotoIndexCode = root.path("data").path("list").get(0).path("personPhoto").path("personPhotoIndexCode").asText();
System.out.println(personPhotoIndexCode);
}
}
```
输出结果为:7114be-c23a-49a2-96bf-52e2f12311d
阅读全文