Java 如何从字符串或json对象中搜索和打印特定内容

Java 如何从字符串或json对象中搜索和打印特定内容,java,android,json,string,Java,Android,Json,String,我想搜索并打印我正在从rest API响应中存储的jsonobject或字符串的值。我想打印详细信息的值,该值为“不允许“暂时在新西兰”状态的应用程序” 输出和响应 {"errors":[{"code":"PredicateValidator","detail":"Application with 'Temporarily in New Zealand' status is not allowed","source":{"source":"/PersonApplicant[0]/Resi

我想搜索并打印我正在从rest API响应中存储的jsonobject或字符串的值。我想打印详细信息的值,该值为
“不允许“暂时在新西兰”状态的应用程序”

输出和响应

    {"errors":[{"code":"PredicateValidator","detail":"Application with 'Temporarily in New Zealand' status is not allowed","source":{"source":"/PersonApplicant[0]/ResidencyStatus"},"title":"Application with 'Temporarily in New Zealand' status is not allowed"}]}'

您可以通过调用
来访问JSON对象的属性。似乎您有一个JSON包装在一个JSON包装的列表中,所以请尝试:

your_json_obj.errors[0].detail
那就给你

"Application with 'Temporarily in New Zealand' status is not allowed"

您可以通过调用
来访问JSON对象的属性。似乎您有一个JSON包装在一个JSON包装的列表中,所以请尝试:

your_json_obj.errors[0].detail
那就给你

"Application with 'Temporarily in New Zealand' status is not allowed"
您可以使用该类从JSON对象访问值

JSONObject responseJson = new JSONObject(responseString);
JSONArray jsonArray=responseJson.getJSONArray("errors");
for(int i=0; i<jsonArray.lenght();i++){
  JSONObject jso = jsonArray.getJSONObject(i);
  System.out.println(jso.getString("detail"));
}
JSONObject responseJson=新的JSONObject(responseString);
JSONArray JSONArray=responseJson.getJSONArray(“错误”);
对于(inti=0;i,您可以使用该类从JSON对象访问值

JSONObject responseJson = new JSONObject(responseString);
JSONArray jsonArray=responseJson.getJSONArray("errors");
for(int i=0; i<jsonArray.lenght();i++){
  JSONObject jso = jsonArray.getJSONObject(i);
  System.out.println(jso.getString("detail"));
}
JSONObject responseJson=新的JSONObject(responseString);
JSONArray JSONArray=responseJson.getJSONArray(“错误”);

对于(inti=0;i而不是
JSONObject
,您可以使用内置于Rest-Assured中的
JsonPath

    String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
    JsonPath path = JsonPath.from(responseString);
    System.out.println("The response from API is: " + path.getString("errors[0].title"));

上述内容将获得错误的标题。它也仅在只有1个可用错误时有效。

您可以使用内置于Rest Assured中的
JSONObject
而不是
JsonPath

    String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
    JsonPath path = JsonPath.from(responseString);
    System.out.println("The response from API is: " + path.getString("errors[0].title"));

以上将获得错误的标题。它也仅在只有1个可用错误的情况下才有效。

感谢您的回复,但“'JSONObject responseJson=new JSONObject(responseString);JSONArray JSONArray=responseJson.getJSONArray(“错误”);for(JSONObject JSONObject:JSONArray){System.out.println(JSONObject.getJsonString(“细节”);}我在jsonarray中的for循环和SOP statment中的getjsonString中收到错误消息。类型不匹配:无法将方法getjsonString(String)从元素类型对象转换为JSONObject对于类型JSONObjectUpdated代码未定义,请重试now@akshay202-非常感谢,现在它可以工作了感谢回复,但是“'JSONObject responseJson=newjsonobject(responseString);JSONArray JSONArray=responseJson.getJSONArray(“错误”);for(JSONObject JSONObject:JSONArray){System.out.println(JSONObject.getJsonString(“细节”);}我在jsonarray中的for循环和SOP statment中的getjsonString中收到错误消息。类型不匹配:无法将方法getjsonString(String)从元素类型对象转换为JSONObject对于类型JSONObjectUpdated代码未定义,请重试now@akshay202-非常感谢,现在它工作了这是抛出错误-因为我无法使用,直接作为jsonobject.errors[0]。详细信息请共享代码谢谢这是抛出错误-因为我无法使用,直接作为jsonobject.errors[0].详情请分享代码谢谢