I have a json string in this format :
[
{
"key1": { "key1":"val1","key2":"val2" },
"key2": { "key1":"val1","key2":"val2" }
}
]
To parse it I created a java class :
class data {
String key;
List <content> listdata;
/* getter and setter for the attribute above */
...
}
Now I followed Gson documentation, and try to exract data :
Gson gson =new Gson();
data[] ints = gson.fromJson(MyjsonString, data[].class);
I get parsing error from Gson API, what I done wrong ?
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path
For parsing json
like following class
class data {
String key;
List <content> listdata;
/* getter and setter for the attribute above */
...
}
you need a json
like as follows
"data" : {
"key": "value",
"listdata": [{ content object key attributes here }]
}
As your exception clearly saying BEGIN_ARRAY but was STRING
so you have to use array in json
for parsing.
In Reply of your comment
If you want a class which parse following json
{
"key1": { "key1":"val1","key2":"val2" },
"key2": { "key1":"val1","key2":"val2" }
}
Then you would have classes structure as follows
class Data {
Map<String, String> key1;
Map<String, String> key2;
/* getter and setter for the attribute above */
}
As per the Json, this class structure works:
class data {
List <Map<String,Content> data;
/* getter and setter for the attribute above */
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.