猿问

java 里面的 Gson 问题

@Test
publicvoidtestGetInt(){
Usersusers=usersDAO.get(3);
StringjsonUsers=gson.toJson(users);
System.out.println("jsonUsers:"+jsonUsers);
intavatarId=users.getAvatarId();
Avataravatar=avatarDAO.get(avatarId);
StringjsonAvatar=gson.toJson(avatar);
System.out.println("jsonAvatar:"+jsonAvatar);
}
打印的数据:
jsonUsers:
{"user_id":3,"username":"Tony","urlname":"comq"}
jsonAvatar:
{"id":3,"farm":"farm1","bucket":"hbimg","key":"e925755184a-aSvujx","type":"image/jpeg","width":100,"height":100,"frames":1}
怎么把jsonAvatar数据放到jsonUsers里面,变成:
{"user_id":3,"username":"Tony","urlname":"comq","avatar":{"id":3,"farm":"farm1","bucket":"hbimg","key":"e925755184a-aSvujx","type":"image/jpeg","width":100,"height":100,"frames":1}}
九州编程
浏览 335回答 1
1回答

一只斗牛犬

packagecom;importcom.google.gson.Gson;publicclassTestGson{publicstaticvoidmain(Stringargs[]){StringjsonAvatar="";//你的json数据Useruser=newUser();user.setUser_id(3);user.setUsername("Tony");user.setUrlname("comq");user.setAvatar(jsonAvatar);Gsongson=newGson();Stringresult=gson.toJson(user);System.out.println(result);}}packagecom;publicclassUser{privateintuser_id;privateStringusername;privateStringurlname;privateStringavatar;publicintgetUser_id(){returnuser_id;}publicvoidsetUser_id(intuser_id){this.user_id=user_id;}publicStringgetUsername(){returnusername;}publicvoidsetUsername(Stringusername){this.username=username;}publicStringgetUrlname(){returnurlname;}publicvoidsetUrlname(Stringurlname){this.urlname=urlname;}publicStringgetAvatar(){returnavatar;}publicvoidsetAvatar(Stringavatar){this.avatar=avatar;}}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答