json 与 bean 转换及数组和集合之间转换

json 所依赖的jar包 均可在 jar114.com 下载到
1. commons-beanutils.jar
2. commons-collections-3.2.1.jar
3. commons-lang-2.5.jar
4. commons-logging-1.1.1.jar
5. ezmorph-1.0.6.jar
6. json-lib-2.2.3-jdk15.jar


//一般使用方法
JSONArray jsonArray = JSONArray.fromObject(getList());
System.out.println(jsonArray);

转化数组和集合

boolean[] boolArray = new boolean[]{true,false,true};

JSONArray jsonArray = JSONArray.fromObject(boolArray);

System.out.println(jsonArray);

输出:[true,false,true]

List list = new ArrayList();

list.add(“第一个”);

list.add(“第二个”);

JSONArray jsonArray = JSONArray.fromObject(list);

System.out.println(jsonArray);

输出:[“第一个”, “第二个”]

JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']");

System.out.println(jsonArray3);

输出:[“json”, “is”, “'easy'”]

转化对象

转化Map

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

JSONObject json = JSONObject.fromObject(map);

System.out.println(json);

输出:[“name”: “json”, “bool”:true, “int”,1, “arr”:[ “a”, “b”], “func”:function(i){return this.arr[i];}]

转化Bean

MyBean.java

public class MyBean {

private String name = "json";

private int pojoId = 1;

private String func1 = "function(i){return this.options[i]}";

private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");

//以下为get、set方法

}

MyBean bean = new MyBean();

JSONObject jsonObject = JSONObject.fromObject(bean);

System.out.println(jsonObject);

输出:

{"func1":function(i){return this.options[i]},"pojoId":1,"name":"json","func2":function(i){ return this.options[i]; }

从JSON到Beans

//转化为动态bean

String myjson = "{name=\"json\",bool:true,int:1,double:2.2,function:function(a){return a;},array:[1,2]}";

JSONObject json1 = JSONObject.fromString(myjson);

Object bean1 = JSONObject.toBean(json1);

由JSON生成XML

JSONObject json = new JSONObject(true);

XMLSerializer xmlSerializer = new XMLSerializer();

String xml = xmlSerializer.write(json);

System.out.println("xml:" + xml);

输出:xml:<?xml version="1.0" encoding="UTF-8"?>

<o null="true"/>

JSONObject json2 = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");

String xml2 = xmlSerializer.write(json2);

System.out.println("xml2:" + xml2);

输出:xml2:<?xml version="1.0" encoding="UTF-8"?>

<o><bool type="boolean">true</bool><int type="number">1</int><name type="string">json</name></o>

JSONArray json3 = JSONArray.fromObject("[1,2,3]");

String xml3 = xmlSerializer.write(json3);

System.out.println("xml3:" + xml3);

输出:xml3:<?xml version="1.0" encoding="UTF-8"?>

<a><e type="number">1</e><e type="number">2</e><e type="number">3</e></a>

以下来自: http://json-lib.sourceforge.net/usage.html

From JSON to Beans
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";

JSONObject jsonObject = JSONObject.fromObject( json ); 

Object bean = JSONObject.toBean( jsonObject );

assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) ); 

assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );

assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) ); 

assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) ); 

assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) ); 

List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );

 Assertions.assertListEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) ); 

Convert to Bean:
String json = "{bool:true,integer:1,string:\"json\"}"; 

JSONObject jsonObject = JSONObject.fromObject( json ); 

BeanA bean = (BeanA) JSONObject.toBean( jsonObject, BeanA.class ); 

assertEquals( jsonObject.get( "bool" ), Boolean.valueOf( bean.isBool() ) ); 

assertEquals( jsonObject.get( "integer" ), new Integer( bean.getInteger() ) ); 

assertEquals( jsonObject.get( "string" ), bean.getString() ); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值