我有一个JSON文件是这样的:使用GSON解析JSON数组
[
{
"number": "3",
"title": "hello_world",
}, {
"number": "2",
"title": "hello_world",
}
]
之前文件时有一个根元素,我会用:
Wrapper w = gson.fromJson(JSONSTRING, Wrapper.class);
代码,但我想不出如何编写Wrapper
类作为根元素是一个数组。
我已经尝试使用:
Wrapper[] wrapper = gson.fromJson(jsonLine, Wrapper[].class);
有:
但还没有任何运气。我怎么才能使用这种方法读取这个?
P.S我有这个利用工作:
JsonArray entries = (JsonArray) new JsonParser().parse(jsonLine);
String title = ((JsonObject)entries.get(0)).get("title");
但是我宁愿知道如何使用这两种方法做到这一点(如果可能)。
你肯定有标题元素后,逗号?如果你删除它们'Wrapper [] data = gson.fromJson(jElement,Wrapper []。class);'对我来说工作正常。 – Pshemo
这就是问题..这么简单的错误! – Edd