【发布时间】:2022-01-23 09:49:54
【问题描述】:
大家好,我有这样的 json 响应
Response response = service.execute(requestSendToNS);
// JSONObject jsonResponse = new JSONObject(response);
//String data = jsonResponse.getString("id");
System.out.println("Response Body : " + response.getBody());
结果如下:
Response Body : [{"status":"success","message":"update success","id":"1404","internalid":2604},{"status":"failed","message":"bad location is already used in another location","id":1405}]
我的问题是如何从我的 json 响应中获取值“id”?
我已尝试使用此代码:
// JSONObject jsonResponse = new JSONObject(response);
//String data = jsonResponse.getString("id");
我也用过这个 列出 responseObject = objectMapper.readValue(response.getBody(),List);
但我无法从 json 数组映射到对象
但我无法从 id 中检索值
【问题讨论】:
-
你可以使用像 Gson 或 ObjectMapper 这样的 json 序列化器/反序列化器库
-
你可以使用
com.fasterxml.jackson.databind.ObjectMapper,它有一个方法readValue,它将接受final String json, Class<T> type并返回<T>。