I have search for many similar questions “Expected BEGIN_OBJECT but was STRING” but nothing work for me.
Here is my json string:
{
"result": "true",
"data": [{
"from": {
"user_id": "user1",
"name": "user1",
"avatar": "http://zzzzzz.jpg"
},
"request_id": "user1:user2"
},
{
"from": "null",
"request_id": "user1:user3"
}]
}
This is my mapping entities:
public class GetFriendResponse {
public boolean result;
public List<FriendRequest> data;
}
public class FriendRequest {
public String request_id;
public UserInfo from;
}
public class UserInfo {
public String user_id;
public String name;
public String avatar;
}
And I parse json like this:
Gson gson = new Gson();
GetFriendResponse response = gson.fromJson(jsonString,GetFriendResponse.class);
I have check and see the problem is when the result contains "from": "null", it give an error.
Is there any idea?
You should make the webservice return null or {}, not “null”.