天天看點

java 字元串轉gson對象_gson字元串 複雜對象 互轉 請用 gson

{"msglis":{"[email protected]":[{"userName":"lhf"}]}}UserInfo對象的屬性privateStringid;privateStringuserName;privateStringpassword;privateStringpirture;privateIn...

{"msglis":{"[email protected]":[{"userName":"lhf"}]}}

UserInfo 對象的屬性

private String id;

private String userName;

private String password;

private String pirture;

private Integer state;

Room 對象的屬性

private String id;

private String name;

public class MsgEntity {

private Map > msglis;//使用者房間資訊集合

private UserInfo user;//目前使用者

private Room room;//目前房間

//get and set 省略

}

//下面是我的錯誤

MsgEntity msgentity = new MsgEntity();

UserInfo u = new UserInfo();

List list = new ArrayList();

u.setUserName("lhf");

list.add(u);

Room r = new Room();

r.setName("lhf");

System.out.println("toString" +new Gson().toJson(msgentity));

//這句輸出:{"msglis":{"[email protected]":[{"userName":"lhf"}]}}

System.out.println("rtoString" +new Gson().fromJson(new Gson().toJson(msgentity), MsgEntity.class));

//報 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 13 異常

求高手指點

展開