我通过使用java使用restAssured点击post请求,从服务器获取json格式的响应。
Response response = requestSpecification.body(text).post(endpoint);
现在我想获取名为“freebies”的子 json 对象。我如何执行此操作,因为当我写作时:
JSONObject responseJSONObject = new JSONObject(response);
//jsonObject.getJSONObject("freebies").getString("id");
JSONArray list = jsonObject.getJSONArray("freebies");
String freebies = list.getJSONObject(0).getString("id");
for (int i = 0; i < freebies.length(); i++) {
String id = list.getJSONObject(i).getString("id");
System.out.println(id.toString());
String name = list.getJSONObject(i).getString("name");
System.out.println(name.toString());
String packName = list.getJSONObject(i).getString("packName");
System.out.println(packName.toString());
String quota = list.getJSONObject(i).getString("quota");
System.out.println(quota.toString());
我收到 stackoverFlow 错误。
请帮忙。
最佳答案
尝试使用下面的行将放心的响应正文转换为 JSONObject 格式:
JSONObject responseJSONObject = new JSONObject(response.getBody().asString);
关于java - 无法从放心响应中获取 Json 对象(json 格式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47958699/