您好我已经使用GSON解析简单的jsonobjects。但这次我的回复数据非常复杂。所以我很难解析那些数据。我的数据看起来像:
[{"creator":"1", "users":[{"userName":"nilesh", "userAge":"25"},{"userName":"Me", "userAge":"25"}]},
{"creator":"2", "users":[{"userName":"nilesh", "userAge":"25"},{"userName":"Me", "userAge":"25"}]}
]
所以我用以下方式编写了一个解析类
public class UserData
{
@SerializedName("creator")
public String creator;
public List<Users> users;
public static class Users
{
@SerializedName("userName")
public String userName;
@SerializedName("userAge")
public String userAge;
}
}
但这不起作用。我做错了什么需要一些关于这个解析的帮助。谢谢。
我试着像这样解析:
Gson gson = new Gson();
UserData users = gson.fromJson(result, UserData.class);
它给我这样的错误:
01-04 12:36:04.337: E/AndroidRuntime(15651): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
答案 0 :(得分:-1)
这是因为您发布的JSON无效。
它应该是这样的下面发布的json是你的json有效。
[
{
"creator": "1",
"users": [
{
"userName": "nilesh",
"userAge": "25"
},
{
"userName": "Me",
"userAge": "25"
}
]
},
{
"creator": "2",
"users": [
{
"userName": "nilesh",
"userAge": "25"
},
{
"userName": "Me",
"userAge": "25"
}
]
}
]
您发布的json包含以下几点的错误
users [
应为"users" :[
userAge :
应为"userAge":
答案 1 :(得分:-1)
这就是我要找的东西
public class UserData
{
public String creator;
public List<Users> user;
public class Users
{
public String userName;
public String userAge;
}
}
我按照以下方式解析
Gson gson = new Gson();
Type type = new TypeToken<List<UserData>>(){}.getType();
List<UserData > objList = gson.fromJson(result, type);