json-lib依赖
json-lib-2.3-jdk15.jar ======================= commons-collections-3.1.jar commons-beanutils.jar ezmorph-1.0.4.jar commons-logging-1.1.jar commons-lang-2.3.jar xom-1.1.jar
view plain copy to clipboard print ?
package json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
public class JSONTest {
private static String jsonStr = "{/"id/": 1, /"name/": /"haha/", /"email/": [{/"id/": 1, /"address/": /"[email protected] /"}, {/"id/": 2, /"address/": /"[email protected] /"}]}" ;
public static class User {
private String name;
private int age;
public User() {
}
public User(String n, int a) {
this .name = n;
this .age = a;
}
public void setName(String n) {
this .name = n;
}
public void setAge( int a) {
this .age = a;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
private static User[] getUsers() {
return new User[] {
new User( "name1" , 11 ),
new User( "name2" , 22 ),
new User( "name3" , 33 )
};
}
private static void printValueFromJsonStr() {
System.out.println(jsonStr);
JSONObject jsonObj = JSONObject.fromObject(jsonStr);
String name = jsonObj.getString("name" );
System.out.println("name=" + name);
JSONArray jsonArr = jsonObj.getJSONArray("email" );
for ( int i = 0 ; i < jsonArr.size(); i++) {
JSONObject jObj = jsonArr.getJSONObject(i);
System.out.println("address=" + jObj.getString( "address" ));
}
}
private static void strToJsonArray() {
String str = "['1','2','3']" ;
JSONArray arr = JSONArray.fromObject(str);
System.out.println("strToJsonArray=" + arr.toString());
}
private static void printValueFromJsonStr1() {
System.out.println(jsonStr);
JSONObject jsonObj = JSONObject.fromObject(jsonStr);
Object bean = JSONObject.toBean(jsonObj);
try {
String name = (String)PropertyUtils.getProperty(bean, "name" );
Object email = PropertyUtils.getIndexedProperty(bean, "email" , 1 );
String addr = (String)PropertyUtils.getProperty(email, "address" );
System.out.println("class=" + email.getClass() + ";addr=" + addr);
MorphDynaBean eBean = (MorphDynaBean)email;
System.out.println("printValueFromJsonStr1=" + name);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void strToBean() {
String str = "{/"name/": /"haha/", /"age/": 26}" ;
JSONObject obj = JSONObject.fromObject(str);
User bean = (User)obj.toBean(obj, User.class );
System.out.println("name=" + bean.getName() + ";age=" + bean.getAge());
}
private static void mapToJson() {
Map map = new HashMap();
map.put("key1" , "value1" );
map.put("key2" , "value2" );
map.put("key3" , "value3" );
JSONObject jsonObj = JSONObject.fromObject(map);
System.out.println("Map=" + jsonObj.toString());
}
private static void arrayToJson() {
User[] users = getUsers();
JSONArray jsonArr = JSONArray.fromObject(users);
System.out.println("Array=" + jsonArr.toString());
}
private static void listToJson() {
User[] users = getUsers();
List data = new ArrayList();
for ( int i = 0 ; i < users.length; i++) {
data.add(users[i]);
}
JSONArray jsonArr = JSONArray.fromObject(data);
System.out.println("List=" + jsonArr.toString());
}
private static void objectToJson() {
User user = new User( "name" , 26 );
JSONObject jsonObj = JSONObject.fromObject(user);
System.out.println("Object=" + jsonObj.toString());
}
private static void combineToJson() {
User[] users = getUsers();
Map map = new HashMap();
map.put("users" , users);
map.put("key1" , "value1" );
List list = new ArrayList();
list.add(map);
list.add("test" );
Map outerMap = new HashMap();
outerMap.put("list" , list);
outerMap.put("flag" , "outer" );
JSONObject jsonObj = JSONObject.fromObject(outerMap);
System.out.println("Combine=" + jsonObj.toString());
}
private static void jsonToXml() {
System.out.println(jsonStr);
JSONObject jsonObj = JSONObject.fromObject(jsonStr);
XMLSerializer x = new XMLSerializer();
String xml = x.write(jsonObj);
System.out.println("toXml=" + xml);
}
private static void xmlToJson() {
String xml = "[email protected] 1 " ;
XMLSerializer x = new XMLSerializer();
JSON json = x.read(xml);
System.out.println("toJson=" + json.toString());
}
public static void main(String[] args) {
printValueFromJsonStr();
printValueFromJsonStr1();
strToJsonArray();
strToBean();
mapToJson();
arrayToJson();
listToJson();
objectToJson();
combineToJson();
jsonToXml();
xmlToJson();
}
}
printValueFromJsonStr方法结果:
{"id": 1, "name": "haha", "email": [{"id": 1, "address": "[email protected] "}, {"id": 2, "address": "[email protected] "}]}
name=haha[email protected] [email protected]
printValueFromJsonStr1方法结果:
{"id": 1, "name": "haha", "email": [{"id": 1, "address": "[email protected] "}, {"id": 2, "address": "[email protected] "}]} class=class net.sf.ezmorph.bean.MorphDynaBean;[email protected] printValueFromJsonStr1=haha
strToJsonArray方法结果:
strToJsonArray=["1","2","3"]
strToBean结果:
name=haha;age=26
mapToJson结果:
Map={"key3":"value3","key2":"value2","key1":"value1"}
arrayToJson结果:
Array=[{"age":11,"name":"name1"},{"age":22,"name":"name2"},{"age":33,"name":"name3"}]
listToJson结果:
List=[{"age":11,"name":"name1"},{"age":22,"name":"name2"},{"age":33,"name":"name3"}]
objectToJson结果:
Object={"age":26,"name":"name"}
combineToJson结果:
Combine={"flag":"outer","list":[{"users":[{"age":11,"name":"name1"},{"age":22,"name":"name2"},{"age":33,"name":"name3"}],"key1":"value1"},"test"]}
jsonToXml结果:
{"id": 1, "name": "haha", "email": [{"id": 1, "address": "[email protected] "}, {"id": 2, "address": "[email protected] "}]} toXml=[email protected] 1 [email protected] 2 1 haha
xmlToJson结果:
toJson={"email":[{"address":"[email protected] ","id":1}]}