JsonNode 读取数据

jsonResponse =
{
    "status":"success",
    "data":{
        "id":1,
        "dealerInfoId":1,
        "dealerName":"北京长怡汽车销售服务有限公司",
        "address":"广州市",
        "active":true,
        "createdAt":"2019-01-24 18:28:45",
        "updatedAt":"2019-01-24 18:28:50",
        "stockImages":null,
        "imageIds":"[1, 2]"
    },
    "message":"",
    "code":0
}

现在有一个Json对象如上所示,那么应该怎么读取data.imageIds呢?

import com.fasterxml.jackson.databind.ObjectMapper;

@Autowired
private ObjectMapper mObjectMapper;


JsonNode jsonNode = mObjectMapper.readTree(jsonResponse).findPath("data").get("imageIds");

String imageIds = jsonNode.traverse(mObjectMapper).readValueAs(String.class);

同理,可获取imageIds同级的键值!

你可能感兴趣的:(Java,ObjectMapper)