发布网友 发布时间:2022-04-24 14:30
共3个回答
热心网友 时间:2023-10-16 11:22
先把user对象转换成字符串比如:
tring jsonStr = "{\"age\":23,\"id\":123,\"name\":\"tt_2009\"," +
"\"province\":\"上海\",\"sex\":\"男\"}";
然后用JSONobject对象的fromobject方法封装一下;追问我的user对象里还有一个对象,按照你这样的方法会报错
热心网友 时间:2023-10-16 11:22
Type type = new TypeToken<Dept>(){}.getType();
Gson gson = new GsonBuilder() .create(); String json = gson.toJson(d, type );追问不好意思 ,看不太懂 ,能说明一下吗
追答
下载gson
Dept d =new Dept("dept1", new User("user1"));Gson gson = new GsonBuilder() .create();
Type type = new TypeToken<Dept>(){}.getType();
System.out.println("toJson "+gson.toJson(d, type));
热心网友 时间:2023-10-16 11:23
Dept d =new Dept("dept1", new User("user1"));
1
2
3
Gson gson = new GsonBuilder() .create();
Type type = new TypeToken<Dept>(){}.getType();
System.out.println("toJson "+gson.toJson(d, type));