【发布时间】:2015-06-05 05:29:31
【问题描述】:
我以这种方式拥有 Json 字符串,
json= [{"id":"1","label":"2","code":"3"},{"id":"4","label":"5","code":"6"}]
我尝试使用 Gson 以这种方式将其转换为 Java 对象,
还有一个名为 Item.java 的 Pojo,其中包含 id、label、code 和 getter setter 字段。
String id;
String label;
String code;
//getter setters
Gson gson = new Gson();
List<Item> items = gson.fromJson(json, new TypeToken<List<Item>>(){}.getType());
然后以这种方式将Java Object转换为List,
List<String> strings = new ArrayList<String>();
for (Object object : items) {
strings.add(object != null ? object.toString() : null);
}
我的输出是这样的,
[Item [id=1, label=2, code=3], Item [id=6, label=5, code=6]
但我需要它作为 List<List<String>> 并且没有 [Items] 即,
[[id=1, label=2, code=3],[id=4, label=5, code=6]]
or direct
List<List<String>>
没有钥匙。
[[1, 2, 3],[4, 5, 6]]
我错过了什么?有人可以帮助我吗?
【问题讨论】:
-
您真的需要
List<List<String>>,还是只是您不喜欢Item#toString()格式化数据的方式? -
我真的没有在这里看到列表。我个人认为你不需要一个。
Item的toString方法是什么样的?
标签: java json string arraylist gson