首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

json与java交换

2012-10-30 
json与java互换下载?json-lib依赖包: commons-beanutils-1.8.0.jar commons-collections-3.2.1.jar common

json与java互换

下载

?

json-lib依赖包:

    commons-beanutils-1.8.0.jar commons-collections-3.2.1.jar commons-lang-2.4.jar commons-logging-1.1.1.jar ezmorph-1.0.6.jar json-lib-2.3-jdk15.jar xom-1.2.2.jar

?

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=

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"));

}

}

?

热点排行