使用json-lib 把javabean转换成json串


(1). jar包
    * 略


. (2)核心类
  * JSONObject --> Map
    > toString();
    > JSONObject map = JSONObject.fromObject(person):把对象转换成JSONObject对象
  * JSONArray --> List
    > toString()
    > JSONArray jsonArray = JSONObject.fromObject(list):把list转换成JSONArray对象



示例代码


(一)测试代码


package test.test.demo1;

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

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


import org.junit.Test;


/**
 * 演示JSON-LIB小工具 
 * @author cxf
 *
 */
public class Demo1 {
/*
* 当map来用
*/
@Test
public void fun1() {
JSONObject map = new JSONObject();
map.put("name", "zhangSan");
map.put("age", 23);
map.put("sex", "male");

String s = map.toString();
System.out.println(s);
}

/*
* 当你已经有一个Person对象时,可以把Person转换成JSONObject对象
*/
@Test
public void fun2() {
Person p = new Person("liSi", 32, "female");
// 把对象转换成JSONObject类型
JSONObject map = JSONObject.fromObject(p);
System.out.println(map.toString());
}

/**
* JSONArray
*/
@Test
public void fun3() {
Person p1 = new Person("zhangSan", 23, "male");
Person p2 = new Person("liSi", 32, "female");

JSONArray list = new JSONArray();
list.add(p1);
list.add(p2);

System.out.println(list.toString());
}

/**
* 原来就有一个List,我们需要把List转换成JSONArray
*/
@Test
public void fun4() {
Person p1 = new Person("zhangSan", 23, "male");
Person p2 = new Person("liSi", 32, "female");
List list = new ArrayList();
list.add(p1);
list.add(p2);


System.out.println(JSONArray.fromObject(list).toString());
}
}



(二)person类


package cn.itcast.demo1;

public class Person {
private String name;
private int age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Person() {
super();
}
public Person(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}


}





你可能感兴趣的:(Java)