java对象和json数据转换实现方式1-使用json-lib实现-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

java对象和json数据转换实现方式1-使用json-lib实现

简介:

测试代码:


package com.yanek.util.json;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JSONLibUtil {

	/**
	 * @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:::"+beanlist2Json(RoomTypeVOs));
		
		String s2=beanlist2Json(RoomTypeVOs);

		Object[] list=getDTOArray(s2,RoomTypeVO.class);
		
		for(Object v:list)
		{
			RoomTypeVO rt=(RoomTypeVO)v;
			System.out.println(rt.getTypeid()+"-"+rt.getTypename()+"-"+rt.getPrice());
		}
		
		

	}
	
	
	public static String bean2Json(Object obj){
        JSONObject jsonObject=JSONObject.fromObject(obj);
        return jsonObject.toString();
    }
	
	public static String beanlist2Json(Object obj){
		JSONArray jsonObject=JSONArray.fromObject(obj);
        return jsonObject.toString();
    }
    
    @SuppressWarnings("unchecked")
    public static <T> T json2Bean(String jsonStr,Class<T> objClass){
        return (T)JSONObject.toBean(JSONObject.fromObject(jsonStr), objClass);
    }
    
    @SuppressWarnings("unchecked")
    public static <T> T jsonarray2Bean(String jsonStr,Class<T> objClass){
        return (T)JSONObject.toBean(JSONObject.fromObject(jsonStr), objClass);
    }

    /** 
     * 从一个JSON数组得到一个java对象数组,形如: 
     * [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...] 
     * @param object 
     * @param clazz 
     * @return 
     */  
     public static Object[] getDTOArray(String jsonString, Class clazz){  
        
          JSONArray array = JSONArray.fromObject(jsonString);  
         Object[] obj = new Object[array.size()];  
              for(int i = 0; i < array.size(); i++){  
                   JSONObject jsonObject = array.getJSONObject(i);  
                  obj[i] = JSONObject.toBean(jsonObject, clazz);  
              }  
              return obj;  
    }

}

输出如下:

json1:::{"id":1,"name":"name1","roomTypeVOs":[{"price":"20","typeid":1,"typename":"nnn1"},{"price":"20","typeid":1,"typename":"nnn1"}]}
json2:::{"id":1,"name":"name1","roomTypeVOs":[{"price":"20","typeid":1,"typename":"nnn1"},{"price":"20","typeid":1,"typename":"nnn1"}]}
json3:::[{"price":"20","typeid":1,"typename":"nnn1"},{"price":"20","typeid":1,"typename":"nnn1"}]
1-nnn1-20
1-nnn1-20

相关java实体类:

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;

}



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;
	

}



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章