下载
java转json
通常用可以用两个不同的类可以完成转换:JSONObject,JSONSerializer
public class Address {
private String street;// 街道
private String city;// 城市
private int zip;// 邮编
private String tel;// 第一个电话号码
private String telTwo;// 第二个电话号码
public Address() {
}
public Address(String street, String city, int zip, String tel,
String telTwo) {
this.street = street;
this.city = city;
this.zip = zip;
this.tel = tel;
this.telTwo = telTwo;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getZip() {
return zip;
}
public void setZip(int zip) {
this.zip = zip;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getTelTwo() {
return telTwo;
}
public void setTelTwo(String telTwo) {
this.telTwo = telTwo;
}
}
//处用JSONObject从java转换json
public class JSONObjectTest extends TestCase {
public void testDate2Json() {
JSONObject jsonObj = JSONObject.fromObject(new Date());
System.out.println(jsonObj.toString());
}
public void testArray2Json() {
JSONArray jarray = JSONArray.fromObject(new String[][] {
{ "one", "two" }, { "three", "four" } });
System.out.println(jarray.toString());
JSONArray arr = JSONArray.fromObject(jarray.toString());
System.out.println(((JSONArray) arr.get(1)).get(0));
}
public void testList2Json() {
List list = new ArrayList();
list.add(new Integer(1));
list.add(new Boolean(true));
list.add(new Character('j'));
list.add(new char[] { 'j', 's', 'o', 'n' });
list.add(null);
list.add("json");
list.add(new String[] { "json", "-", "lib" });
list.add(new JSONFunction(new String[] { "i" }, "alert(i)"));
list.add(new Address("P.O BOX 54534", "Seattle, WA", 42452,
"561-832-3180", "531-133-9098"));
JSONArray arr=JSONArray.fromObject(list);
System.out.println(arr.toString(4));
arr=JSONArray.fromObject(list);
System.out.println(((JSONArray)arr.get(6)).get(0));
System.out.println(((JSONObject)arr.get(8)).get("city"));
}
public void testMap2json(){
Mapmap=newLinkedHashMap();
map.put("integer",newInteger(1));
map.put("boolean",newBoolean(true));
map.put("char",newCharacter('j')); map.put("charArr",newchar[]{'j','s','o','n'});
JSONObjectobj=JSONObject.fromObject(map);
System.out.println(obj.get("integer");
}
public void testBean2Json(){
Addressbean=newAddress();
//设置bean的属性
JSONObject obj=JSONObject.fromObject(bean);
bean=(Bean) obj.toBean(obj, Bean.class);
System.out.println(bean);
}
}
//JSONObject 可以完成除了数组与Map的的转换,JSONArray可以完成数组,List,Set的转换。
}
//利用JSONSerialize 从java转换JSON
public class JSONSerializerTest extends TestCase {
public void testArraytoJson() {
String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}";
JSONObject obj = (JSONObject) JSONSerializer.toJSON(str);
System.out.println(obj.get("string"));
}
public void testMaptoJson() {
Map map = new HashMap();
map.put("string", "JSON");
map.put("integer", "1");
map.put("double", "2.0");
map.put("boolean", "true");
JSONObject obj = (JSONObject) JSONSerializer.toJSON(map);
System.out.println(obj.getString("string"));
System.out.println(obj.getInt("integer"));
}
public void testBeantoJson() {
MyJavaBean bean = new MyJavaBean();
bean.setString("JSON");
bean.setInteger(1);
bean.setDooble(2.0d);
bean.setBool(true);
JSONObject obj = (JSONObject) JSONSerializer.toJSON(bean);
System.out.println(obj.getString("string"));
}
public void testCollectiontoJsonArray(){
Object[] array = new Object[]{ "JSON", "1", "2.0", "true" };
JSONArray arr=(JSONArray) JSONSerializer.toJSON(array);
List list=new ArrayList();
list.add("JSON");
list.add("1");
JSONArray arr1=(JSONArray)JSONSerializer.toJSON(list);
System.out.println(arr.getString(0));
System.out.println(arr1.getString(0));
}
public void testbeantoJson(){
MyJavaBean bean = new MyJavaBean();
bean.setString( "JSON" );
bean.setInteger( 1 );
bean.setDooble( 2.0d );
bean.setBool( true );
JSONObject obj=(JSONObject) JSONSerializer.toJSON(bean);
System.out.println(obj.getString("bool"));
}
}
从json转换java主要用JSONSerializer
public class Json2java extends TestCase {
public void testJSONtoBean() {
MyJavaBean bean = new MyJavaBean();
bean.setString("JSON");
bean.setInteger(1);
bean.setDooble(2.0d);
bean.setBool(true);
JSONObject obj=(JSONObject) JSONSerializer.toJSON(bean);
JsonConfig conf=new JsonConfig();
conf.setRootClass(MyJavaBean.class);
MyJavaBean bean1=(MyJavaBean)JSONSerializer.toJava(obj,conf);
System.out.println(bean1.getDooble());
}
public void testjsontoList(){
List input = new ArrayList();
input.add( "JSON" );
input.add( "1" );
input.add( "2.0" );
input.add( "true" );
JSONArray obj=(JSONArray) JSONSerializer.toJSON(input);
List list=(List)JSONSerializer.toJava(obj);
System.out.println(list.get(0));
}
public void testjsontoarray(){
List input = new ArrayList();
input.add( "JSON" );
input.add( "1" );
input.add( "2.0" );
input.add( "true" );
JSONArray jsonarry=(JSONArray) JSONSerializer.toJSON(input);
JsonConfig conf=new JsonConfig();
conf.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY);
Object[] list=(Object[])JSONSerializer.toJava(jsonarry,conf);
System.out.println(list[0]);
}
public void testjsontoMap(){
Map map = new HashMap();
map.put("string", "JSON");
map.put("integer", "1");
map.put("double", "2.0");
map.put("boolean", "true");
JSONObject obj=JSONObject.fromObject(map);
Map m=(Map)JSONObject.toBean(obj,Map.class);
System.out.println(m.get("string"));
}
}