这篇主要介绍一下如何用JSON-lib包把Java中的对象转换为JSON对象。
1、基本的Java类型可以直接用JSONObject对象的put或accumulate方法
JSONObject json=new JSONObject();
json.accumulate("name", "yuan");
int age=999;
json.put("age", age);
2、数组或集合可以先定义一个数组和集合对象,或者定义JSONArray对象
boolean[] boolArray=new boolean[]{true,false,true};
json.put("boolArray", boolArray);
JSONArray intJSONArray=JSONArray.fromObject("[1,2,3]");
json.put("intJSONArray", intJSONArray);
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]; }" );
json.put("map", map);
//or
//JSONObject jsonObject = JSONObject.fromObject( map );
//json.put("map",jsonObject);
3、JavaBean为先定义好一个JavaBean对象,然后用JSONObject.fromObject(boy)方法
Boy boy=new Boy();
JSONObject obj=JSONObject.fromObject(boy);
4、一个完整的示例
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;
/**
* json-lib解析例类:Java对象转换为JSON
* 主jar包:json-lib-2.4.jdk15.jar
* 依赖的jar包:commons-beanutils-1.8.0.jar commons-collections-3.2.1.jar commons-lang-2.5.jar ezmorgh-1.0.6.jar
* @author yuan
*
*/
public class OfficialJSONAssembled {
/**
* 转换一些普通的Java对象为JSON
* @return
*/
public static JSONObject convertToJSON(){
JSONObject json=new JSONObject();
String name="yuan";
json.accumulate("name", name);
int age=999;
json.put("age", age);
boolean[] boolArray=new boolean[]{true,false,true};
json.put("boolArray", boolArray);
JSONArray intJSONArray=JSONArray.fromObject("[1,2,3]");
json.put("intJSONArray", intJSONArray);
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]; }" );
json.put("map", map);
//or
//JSONObject jsonObject = JSONObject.fromObject( map );
//json.put("map",jsonObject);
return json;
}
/**
* JavaBean To JSON
* @return
*/
public static JSONObject javaBeanToJSON(){
Boy boy=new Boy();
boy.setAge(10);
boy.setName("dada");
boy.setSex("male");
Family family=new Family();
family.setFather("ff");
family.setMother("mm");
GirlFriend gf=new GirlFriend();
gf.setBust("0A");
gf.setName("dazhuang");
List
girlfriends=new ArrayList
();
girlfriends.add(gf);
boy.setFamily(family);
boy.setGirlfriends(girlfriends);
JSONObject obj=JSONObject.fromObject(boy);
return obj;
}
public static void main(String[] args) {
JSONObject json=OfficialJSONAssembled.convertToJSON();
System.out.println(json);
System.out.println(json.get("name"));
System.out.println(""+json.getInt("age"));
System.out.println(json.get("boolArray"));
System.out.println(json.get("intJSONArray"));
System.out.println(json.getJSONObject("map").get("func"));
JSONObject boyObj=OfficialJSONAssembled.javaBeanToJSON();
System.out.println(boyObj);
System.out.println(boyObj.get("name")+"的妈妈是"+boyObj.getJSONObject("family").getString("mother")+",他的女朋友大小为"+boyObj.getJSONArray("girlfriends").getJSONObject(0).getString("bust"));
}
}