我有一个这种格式的json字符串:
[
{
"key1": { "key1":"val1","key2":"val2" },
"key2": { "key1":"val1","key2":"val2" }
}
]
要解析它,我创建了一个java类:
class data {
String key;
List <content> listdata;
/* getter and setter for the attribute above */
...
}
现在我关注了Gson文档,并尝试提取数据:
Gson gson =new Gson();
data[] ints = gson.fromJson(MyjsonString, data[].class);
我从Gson API得到解析错误,我做错了什么?
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path
答案 0 :(得分:1)
用于解析json
,如下面的课程
class data {
String key;
List <content> listdata;
/* getter and setter for the attribute above */
...
}
您需要json
,如下所示
"data" : {
"key": "value",
"listdata": [{ content object key attributes here }]
}
正如你的例外清楚地说BEGIN_ARRAY but was STRING
所以你必须在json
中使用数组进行解析。
回复您的评论
如果你想要一个解析json
的类{
"key1": { "key1":"val1","key2":"val2" },
"key2": { "key1":"val1","key2":"val2" }
}
然后你将有如下的类结构
class Data {
Map<String, String> key1;
Map<String, String> key2;
/* getter and setter for the attribute above */
}
答案 1 :(得分:0)
根据Json,这个类结构有效:
class data {
List <Map<String,Content> data;
/* getter and setter for the attribute above */
...
}