gpt4 book ai didi

java - 如何从字符串或 json 对象中搜索并打印特定内容

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:44 29 4
gpt4 key购买 nike

我想搜索并打印我从 REST API 响应存储的 jsonobject 或字符串的值。并且我想打印详细信息的值 "Application with 'Temporarily in New Zealand' status is not allowed"

    String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
JSONObject responseJson = new JSONObject(responseString);
System.out.println("The response from API is:"+ responseJson);

输出和响应

    {"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"}]}'

最佳答案

您可以使用JSONObject类来访问 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"));
}

关于java - 如何从字符串或 json 对象中搜索并打印特定内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56881358/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com