从HttpResponse中查找JSONArray中的特定JSON对象

从HttpResponse中查找JSONArray中的特定JSON对象,json,rest,httpclient,response,Json,Rest,Httpclient,Response,这就是我的代码目前的样子: HttpResponse response = client.execute(request); String responseBody = EntityUtils.toString(response.getEntity()); int statuscode = response.getStatusLine().getStatusCode(); System.out.println(responseBody); 我得到的答复如下所示: {"developerMess

这就是我的代码目前的样子:

HttpResponse response = client.execute(request);
String responseBody = EntityUtils.toString(response.getEntity());
int statuscode = response.getStatusLine().getStatusCode();
System.out.println(responseBody);
我得到的答复如下所示:

{"developerMessage":"The requested resource is not available.",
"httpStatusCode":"404",
"errors":[{"developerMessage":"Savings account with identifier 15 does not exist"
我想要得到的是“标识符为15的储蓄帐户不存在”作为字符串

有什么想法吗?

试试这个

        String jsonString = "{\n" +
                "    \"developerMessage\": \"The requested resource is not available.\",\n" +
                "    \"httpStatusCode\": \"404\",\n" +
                "    \"errors\": [\n" +
                "        {\n" +
                "            \"developerMessage\": \"Savings account with identifier 15 does not exist\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            String string = jsonObject.getString("errors");
            JSONArray jsonArray = new JSONArray(string);
            JSONObject jsonObject2 = jsonArray.getJSONObject(0);
            String message = jsonObject2.getString("developerMessage");
            Log.i("YourApp", message);
        } catch (JSONException e) {
            e.printStackTrace();
        }

我得到以下错误:org.json.JSONException:JSONObject[“errors”]不是字符串。我的代码在发布之前测试正常。我认为您应该检查您的响应消息是否有效。您可以在jsonlint.com上查看。查看我的jsonString,它与您的不同吗?您发布的jsonString是我在println(responseBody)时在java中作为输出得到的,我从字符串responseBody=EntityUtils.toString(response.getEntity())中得到;我不想对错误进行硬编码,我想将错误作为一个响应,并能够使用错误字符串将其插入我的数据库谢谢,虽然有点晚,但随着知识的增加,我更好地理解了你的答案,现在它工作得很好