首页 > 代码库 > java对象和json数据转换实现方式2-使用gson实现
java对象和json数据转换实现方式2-使用gson实现
测试代码:
package com.yanek.util.json; import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonParser; public class GsonUtil { /** * @param args */ public static void main(String[] args) { Hotel h1=new Hotel(); h1.setId(1); h1.setName("name1"); Hotel h2=new Hotel(); h2.setId(2); h2.setName("name2"); RoomTypeVO t1=new RoomTypeVO(); t1.setPrice("20"); t1.setTypeid(1); t1.setTypename("nnn1"); List<RoomTypeVO> RoomTypeVOs=new ArrayList<RoomTypeVO>(); RoomTypeVOs.add(t1); RoomTypeVOs.add(t1); h1.setRoomTypeVOs(RoomTypeVOs); Hotel hotel=new Hotel(); System.out.println("json1:::"+bean2Json(h1)); String s=bean2Json(h1); hotel=json2Bean(s,Hotel.class); System.out.println("json2:::"+bean2Json(hotel)); System.out.println("json3:::"+bean2Json(RoomTypeVOs)); String s2=bean2Json(RoomTypeVOs); List<RoomTypeVO> RoomTypeVOs2=new ArrayList<RoomTypeVO>(); RoomTypeVOs2=json2Bean(s2,List.class); System.out.println("json4:::"+bean2Json(RoomTypeVOs2)); } private static Gson gson = new GsonBuilder().create(); public static String bean2Json(Object obj){ return gson.toJson(obj); } public static <T> T json2Bean(String jsonStr,Class<T> objClass){ return gson.fromJson(jsonStr, objClass); } public static String jsonFormatter(String uglyJsonStr){ Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonParser jp = new JsonParser(); JsonElement je = jp.parse(uglyJsonStr); String prettyJsonString = gson.toJson(je); return prettyJsonString; } }
输出如下:
json1:::{"RoomTypeVOs":[{"typeid":1,"typename":"nnn1","price":"20"},{"typeid":1,"typename":"nnn1","price":"20"}],"id":1,"name":"name1"} json2:::{"RoomTypeVOs":[{"typeid":1,"typename":"nnn1","price":"20"},{"typeid":1,"typename":"nnn1","price":"20"}],"id":1,"name":"name1"} json3:::[{"typeid":1,"typename":"nnn1","price":"20"},{"typeid":1,"typename":"nnn1","price":"20"}] json4:::[{"typeid":1.0,"typename":"nnn1","price":"20"},{"typeid":1.0,"typename":"nnn1","price":"20"}]
相关实体类:
package com.yanek.util.json; import java.util.List; public class Hotel { public List<RoomTypeVO> getRoomTypeVOs() { return RoomTypeVOs; } public void setRoomTypeVOs(List<RoomTypeVO> roomTypeVOs) { RoomTypeVOs = roomTypeVOs; } private List<RoomTypeVO> RoomTypeVOs; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } private int id; private String name; } package com.yanek.util.json; public class RoomTypeVO { public int getTypeid() { return typeid; } public void setTypeid(int typeid) { this.typeid = typeid; } public String getTypename() { return typename; } public void setTypename(String typename) { this.typename = typename; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } private int typeid; private String typename; private String price; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。