• java对象和json数据转换实现方式3-使用jackson实现


    測试代码:

    package com.yanek.util.json;
    
    import java.io.IOException;
    import java.io.StringWriter;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.fasterxml.jackson.core.JsonFactory;
    import com.fasterxml.jackson.core.JsonGenerator;
    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    public class JacksonUtil {
    
    	/**
    	 * @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();
    		
    		try {
    			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));
    			
    			
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    
    	}
    	
    	
    	public static String bean2Json(Object obj) throws IOException {
            ObjectMapper mapper = new ObjectMapper();
            StringWriter sw = new StringWriter();
            JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
            mapper.writeValue(gen, obj);
            gen.close();
            return sw.toString();
        }
    
        public static <T> T json2Bean(String jsonStr, Class<T> objClass)
                throws JsonParseException, JsonMappingException, IOException {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(jsonStr, objClass);
        }
    
    }
    

    输出例如以下:

    json1:::{"id":1,"name":"name1","roomTypeVOs":[{"typeid":1,"typename":"nnn1","price":"20"},{"typeid":1,"typename":"nnn1","price":"20"}]}
    json2:::{"id":1,"name":"name1","roomTypeVOs":[{"typeid":1,"typename":"nnn1","price":"20"},{"typeid":1,"typename":"nnn1","price":"20"}]}
    json3:::[{"typeid":1,"typename":"nnn1","price":"20"},{"typeid":1,"typename":"nnn1","price":"20"}]
    json4:::[{"typeid":1,"typename":"nnn1","price":"20"},{"typeid":1,"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;
    
    }
    
    


  • 相关阅读:
    1010每次备份我的MySQL数据库
    1008win7与虚拟机中的linux共享文件的(详细)方法
    0930MySQL中实现高性能高并发计数器方案(例如文章点击数)
    0929shell操作mysql
    0929mysql前缀索引如何找到合适的位数
    0929mysql 用户管理和权限设置
    学习笔记之机器学习实战 (Machine Learning in Action)
    学习笔记之Python for Data Analysis
    学习笔记之入行数据科学,这些书一定要看
    面试总结之Python
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7325052.html
Copyright © 2020-2023  润新知