java - 使用 javax 时如何更改 JSONObject 的类型
问题描述
我有一个StringBuilder
:
我的代码如下:
String response = sb.toString();
// logger.info("response is '{}' ", response);
JsonObject jsonObject = new JsonObject(response);
JsonObject fileStatuses = jsonObject.getJsonObject("FileStatuses");
JsonArray fileStatus = (JsonArray) fileStatuses.getJsonArray("FileStatus");
for (int i = 0; i < fileStatus.size(); ++i) {
JsonObject rec = fileStatus.getJsonObject(i);
String pathSuffix = rec.getString("pathSuffix");
logger.info(" the pathSuffix value is '{}' ", pathSuffix );
}
我想使用javax.json.JsonObject
而不是org.json.JsonObject
。所以,问题出在new JsonObject(response);
==> Cannot instantiate the type JsonObject
.
我知道它org.json.JSONObject
有一个接受字符串的构造函数,但我认为使用javax.json.JsonObject
.
如何更正我的代码以 JsonObject jsonObject = new JsonObject(response)
获取字符串并继续使用javax.json.JsonObject
类?
解决方案
根据它的 API 而不是使用JsonObject jsonObject = new JsonObject(response);
你可以response
像这样从你的 -String 实例化它:
String response = sb.toString();
StringReader reader = new StringReader(response);
JsonReader jsonReader = Json.createReader(reader);
JsonObject jsonObject = jsonReader.readObject();
jsonReader.close();
推荐阅读
- oracle - 如何使用 AUTO_INCREMENT 以 5 创建 id 并从 2000 年开始
- selenium - 如何在硒黄瓜框架中使用Json进行参数化
- c# - C#:在给定(繁忙)索引中添加值,将其他值推到右侧
- github - 带有 Github 页面的两级子域
- java - Sonarqube 严重错误:等待 JVM 进程失败 Windows
- java - Selenium [Java]:我怎样才能让它等到表被刷新?
- openmdao - 如何构建使用 MuxComp 将单个变量组合成数组的 OpenMDAO 组?
- c - 寻找一些代码行的解释
- flutter - 颤振项目合并到 GitHub 失败
- java - Maven 无法访问依赖 jar 中的 interface.class