我在输出中获取了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)); }
这是我在输出中的respponse格式。我想从response中获取出价。我可以吗?
[
{
"bName": "abc",
"bId": "n86nbnhbnghgy76"
}
]
答案 0 :(得分:1)
使用JSONArray(String json)
构造函数解码字符串:
String response = response.getBody();
JSONArray res = new JSONArray(response);
String bId = res.getJSONObject(0).get("bId");
System.out.println(bid);
答案 1 :(得分:0)
修改强>
请尝试以下操作:
String response=response.getBody();
JSONObject res = new JSONObject();
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;
res=(JSONObject)array.get(0);
System.out.println(res.get("bId"));
输出
n86nbnhbnghgy76
这是基于您的代码和Simple Json Library
。