1.JAR包简介
要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包:
commons-lang.jar
commons-beanutils.jar
commons-collections.jar
commons-logging.jar
ezmorph.jar
要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包:
commons-lang.jar
commons-beanutils.jar
commons-collections.jar
commons-logging.jar
ezmorph.jar
json-lib-2.2.2-jdk15.jar
下载地址:http://download.csdn.net/download/luming_xml/9521971
简单测试:
测试案例1
package lu.test;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Test {
public static void main(String[]args){
//创建JSONObject
JSONObject jsonObject=new JSONObject();
jsonObject.put("username", "hjm");
jsonObject.put("password", "123");
System.out.println(jsonObject);
//增加属性
jsonObject.element("sex", "男");
System.out.println(jsonObject);
//根据key返回
System.out.println(jsonObject.get("sex"));
//判断输出对象的类型
boolean isArray=jsonObject.isArray();
boolean isEmpty=jsonObject.isEmpty();
boolean isNullObject=jsonObject.isNullObject();
System.out.println("是否数组: "+isArray+" 是否空:"+isEmpty+" 是否空对象:"+isNullObject);
//将JSONArray增加到JSONObject中
JSONArray jsonArray=new JSONArray();
jsonArray.add(0,"hjm");
jsonArray.add(1,"cag");
//开始增加
jsonObject.element("student", jsonArray);
System.out.println(jsonObject);
}
}
<strong>测试案例2</strong>
package lu.test;
import junit.framework.TestCase;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONTest extends TestCase{
public void testJson(){
JSONObject jsonObj0 = new JSONObject();
JSONObject jsonObj = new JSONObject();
JSONObject jsonObj2 = new JSONObject();
JSONObject jsonObj3 = new JSONObject();
JSONArray jsonArray = new JSONArray();
//创建jsonObj0
jsonObj0.put("name0", "zhangsan");
jsonObj0.put("sex1", "female");
System.out.println("jsonObj0:"+jsonObj0);
//创建jsonObj1
jsonObj.put("name", "xuwei");
jsonObj.put("sex", "male");
System.out.println("jsonObj:"+jsonObj);
//创建jsonObj2,包含两个条目,条目内容分别为jsonObj0,jsonObj1
jsonObj2.put("item0", jsonObj0);
jsonObj2.put("item1", jsonObj);
System.out.println("jsonObj2:"+jsonObj2);
//创建jsonObj3,只有一个条目,内容为jsonObj2
jsonObj3.element("j3", jsonObj2);
System.out.println("jsonObj3:"+jsonObj3);
//往JSONArray中添加JSONObject对象。发现JSONArray跟JSONObject的区别就是JSONArray比JSONObject多中括号[]
jsonArray.add(jsonObj);
System.out.println("jsonArray:"+jsonArray);
JSONObject jsonObj4 = new JSONObject();
jsonObj4.element("weather", jsonArray);
System.out.println("jsonObj4:"+jsonObj4);
}
}
测试案例3
package lu.test;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Address {
public String getRoad() {
return road;
}
public void setRoad(String road) {
this.road = road;
}
public String getStreate() {
return streate;
}
public void setStreate(String streate) {
this.streate = streate;
}
public String getProvience() {
return provience;
}
public void setProvience(String provience) {
this.provience = provience;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
private String road;
private String streate;
private String provience;
private String no;
public static void main(String[]args){
//将json对象转换为Java对象
JSONObject jsonObject = JSONObject.fromObject("{\"no\":\"104\",\"provience\":\"陕西\",\"road\":\"高新路\",\"streate\":\"\"}");
Address Address = (Address) JSONObject.toBean(jsonObject,Address.class);
System.out.println(Address.getNo());
System.out.println(Address.getStreate());
System.out.println(Address.getProvience());
System.out.println(Address.getRoad());
//将Java对象转换成json对象;
Address address = new Address();
address.setNo("104");
address.setProvience("陕西");
address.setRoad("高新路");
address.setStreate("");
JSONArray json = JSONArray.fromObject(address);
JSONObject jb=JSONObject.fromObject(address);
System.out.println(json.toString());
System.out.println(jb);
//将JSONArray转化为list
JSONArray jsonArray = new JSONArray();
jsonArray.add("{\"no\":\"104\",\"provience\":\"陕西\",\"road\":\"高新路\",\"streate\":\"\"}");
jsonArray.add("{\"no\":\"104\",\"provience\":\"陕西\",\"road\":\"高新路\",\"streate\":\"123\"}");
Object object = JSONArray.toList(jsonArray,Address.class);
}
}