java对象转换成json数据

编码前还需要导入的jar包: commons-beanutils-1.7.0.jar 、 commons-lang-2.5.jar 、commons-logging-1.0.4.jar 、 ezmorph-1.0.6.jar 、 json-lib-1.1-jdk15.jar、morph-1.1.1.jar
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.ch.entity.JSONTestBean;
import com.ch.entity.Props;

/**
 * Java对象转换为Json对象
 * @author CHH
 * @since 2012-08-04
 */

public class TestJson {

    public static void main(String[] args) {
        TestJson j = new TestJson();
        j.ObjectList2json();
    }

    /**
     * List对象转换为Json
     */
    public void ObjectList2json() {
        Map map = new HashMap();
        List jlist = new ArrayList();
        //JSONTestBean类的构造方法
        JSONTestBean bean1 = new JSONTestBean("zhangbo", "123123");
        JSONTestBean bean2 = new JSONTestBean("lisi", "65489");
        //Props类的构造方法
        Props props = new Props("127.0.0.1", "8008");
        jlist.add(bean1);
        jlist.add(bean2);
        map.put("Props", props);
        map.put("jsonObjectList", jlist);
        JSONArray jsonArray = JSONArray.fromObject(map);
        System.out.println(jsonArray);
    }

    /**
     * 布尔数组转换为Json
     */
    public void arr2json() {
        boolean[] boolArray = new boolean[] { true, false, true };
        JSONArray jsonArray = JSONArray.fromObject(boolArray);
        System.out.println(jsonArray);
        // prints [true,false,true]
    }

    /**
     * List转换为Json
     */
    public void list2json() {
        List list = new ArrayList();
        list.add("first");
        list.add("second");
        JSONArray jsonArray = JSONArray.fromObject(list);
        System.out.println(jsonArray);
        // prints ["first","second"]
    }

    /**
     * 创建Json格式的数据
     */
    public void createJson() {
        JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");
        System.out.println(jsonArray);
        // prints ["json","is","easy"]
    }

    /**
     * Map转换为Json
     */
    public void map2json() {
        Map map = new HashMap();
        map.put("name", "json");
        map.put("bool", Boolean.TRUE);
        map.put("int", new Integer(1));
        map.put("arr", new String[] { "a", "b" });
        map.put("func", "function(i){ return this.arr[i]; }");

        JSONObject json = JSONObject.fromObject(map);
        System.out.println(json);
        // prints
        // ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){
        // return this.arr[i]; }]
    }

    /**
     * 类对象转换为Json
     */
    public void bean2json() {
        JSONObject jsonObject = JSONObject.fromObject(new JSONTestBean(
                "zhangbo", "234234"));
        System.out.println(jsonObject);
        /*
         * prints {"func1":function(i){ return this.options[i];
         * },"pojoId":1,"name":"json","func2":function(i){ return
         * this.options[i]; }}
         */
    }

    /**
     * Json转换为类对象
     */
    public void json2bean() {
        String json = "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}";
        // JSONObject jb = JSONObject.fromString(json);
        // JSONObject.toBean(jb, MyBean.class);
        System.out.println(json);
    }

}
 
<pre class="html" name="code">View JSONTestBean 
 /**
  * 实体类一
  * 
  * @author CHH
  * @since 2012-08-04
  */
 public class JSONTestBean {
 
     private String userName;
     private String password;
 
     public JSONTestBean() {
 
     }
 
     public JSONTestBean(String username, String password) {
         this.userName = username;
         this.password = password;
     }
 
     public String getPassword() {
         return password;
     }
 
     public void setPassword(String password) {
         this.password = password;
     }
 
     public String getUserName() {
         return userName;
     }
 
     public void setUserName(String userName) {
         this.userName = userName;
     }
 }
 
 
 
 
View Props 

/**
 * 
 * @author CHH
 * @since 2012-08-04
 */
public class Props {

    private String ip;
    private String port;

    public Props() {
    }

    public Props(String ip, String port) {
        this.ip = ip;
        this.port = port;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

}


 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值