【发布时间】:2014-09-29 08:04:16
【问题描述】:
我在输出中得到 json 数组。我想从响应中访问特定的关键元素。我怎么能 ..?
ResponseEntity <String> respone;
try {
response =
restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
String response=response.getBody();
JSONObject res = new JSONObject();
res.put("result", response);
System.out.println(res);
int len=res.size();
System.out.println(len);
JSONParser parser=new JSONParser();
Object obj = parser.parse(response);
JSONArray array = (JSONArray)obj;
System.out.println(array.get(0)); }
这是我正在输出的响应格式。我想从响应中访问出价。我该怎么做?
[
{
"bName": "abc",
"bId": "n86nbnhbnghgy76"
}
]
【问题讨论】:
-
杰克逊?格森?帮助您通过元素轻松解析的库
-
@sfat ...我正在使用杰克逊..
-
您正在使用 JSONParser。如果您的项目中有杰克逊,只需执行 ObjectMapper mapper = new ObjectMapper(); AClassThatMatchesTheModelOfThatJson thatModel = mapper.readValue(response, AClassThatMatchesTheModelOfThatJson.class);