从下面的json对象中,我必须使用java提取errorCauses中的errorSummary。我已附上我正在使用的示例代码,因为我收到了 json 异常错误。
{
"errorCode": "xxxx",
"errorSummary": "xxxxxx",
"errorLink": "xxxx",
"errorId": "xxxxx",
"errorCauses": [
{
"errorSummary": "Get the error summary"
}
]
}
我正在使用下面的代码来提取错误原因,但它抛出错误。
public String getExceptionMessage(MyRestTemplateException ex) {
try {
String Json = ex.getMessage();
JSONObject json = new JSONObject(Json);
JSONObject jsonData = new JSONObject();
// String ExceptonMsg = json.getString("errorSummary");
JSONObject errCause = new JSONObject(json.getString("errorCauses"));
LOGGER.info("jsonArr AES " +errCause.toString());
return errCause.toString();
} catch (Exception e) {
LOGGER.error("Error while get exception message: " + e.toString());
}
return null;
}
有人可以帮我解决这个问题吗?
最佳答案
替换
JSONObject errCause = new JSONObject(json.getString("errorCauses"));
与
JSONObject errCause = jsonObj.getJSONArray("errorCauses");
关于java - 使用java从json对象中提取json字符串数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61457996/