【发布时间】:2019-04-05 19:02:11
【问题描述】:
所以我有这个 ArrayList 现有的学校班级,每个班级都有一个 ArrayList 包含附加值的 ArrayLists。我必须像这样声明它
ArrayList<ArrayList<String>>
我将循环遍历这些类,并使用 JsonObjectBuilder 将其添加到 JsonObject 中。这是一个例子。
JsonArrayBuilder arrBuilder = Json.createArrayBuilder();
for (Rota class : rotaArray) {
JsonObjectBuilder builderRota = Json.createObjectBuilder();
String example = class.getExample();
//This is where I get the ArrayList of each class.
ArrayList<ArrayList<String>> arrExample = class.getArrExample();
builderRota
.add("example", example)
//This is where I'm stuck since the builder expects it to have a String as value instead of an ArrayList.
.add("arr", arrExample);
arrBuilder.add(builderRota);
}
现在,我将如何将该 ArrayList 存储在 JsonObject 中,以便以后可以使用它? (我将使用 Polymer 检索此 JsonObject,但首先能够存储数组将是第一步。
提前致谢!
【问题讨论】:
-
你的最终目标是什么?我假设您想将
ArrayList<ArrayList<String>>转换为 json 并可能稍后再读回来? -
@Raj 我的最终目标是稍后在 Polymer 中将其作为 json 对象或 JS 可读的东西读回。我尝试简单地将 toString() 放在 arrExample 后面,但这会使用数据绑定在 Polymer 中返回类似 [[string, 1, string], [string, 2, string]] 的字符串。我也无法用 JSON.parse() 解析这个字符串,因为字符串没有双引号,否则我可能已经解决了一些问题,但我被卡住了