java excel转json_Excel转JSON怎么转

这篇博客详细介绍了如何使用Java类库JSON-lib将Bean、Map和XML数据转换为JSON格式,并反之将JSON转换回Bean和Map。示例代码包括了JSON与boolean数组、List、Map、Bean、Beans以及XML之间的互转,展示了JSON-lib的强大功能。
摘要由CSDN通过智能技术生成

展开全部

JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。

下载地址:http://json-lib.sourceforge.net/

还要需要的第3方包:

org.apache.commons(3.2以上版62616964757a686964616fe78988e69d8331333363376532本)

org.apache.oro

net.sf.ezmorph(ezmorph-1.0.4.jar)

nu.xom

1、List

Java代码

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

JSONArrayjsonArray1=JSONArray.fromObject(boolArray);

System.out.println(jsonArray1);

//prints[true,false,true]

Listlist=newArrayList();

list.add("first");

list.add("second");

JSONArrayjsonArray2=JSONArray.fromObject(list);

System.out.println(jsonArray2);

//prints["first","second"]

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

System.out.println(jsonArray3);

//prints["json","is","easy"]

2、Map

Java代码

Mapmap=newHashMap();

map.put("name","json");

map.put("bool",Boolean.TRUE);

map.put("int",newInteger(1));

map.put("arr",newString[]{"a","b"});

map.put("func","function(i){returnthis.arr[i];}");

JSONObjectjson=JSONObject.fromObject(map);

System.out.println(json);

//{"func":function(i){returnthis.arr[i];},"arr":["a","b"],"int":1,"name":"json","bool":true}

3、BEAN

Java代码

/**

*Bean.java

privateStringname="json";

privateintpojoId=1;

privatechar[]ptions=newchar[]{'a','f'};

privateStringfunc1="function(i){returnthis.options[i];}";

privateJSONFunctionfunc2=newJSONFunction(newString[]{"i"},"returnthis.options[i];");

*/

JSONObjectjsonObject=JSONObject.fromObject(newJsonBean());

System.out.println(jsonObject);

//{"func1":function(i){returnthis.options[i];},"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){returnthis.options[i];}}

4、BEANS

Java代码

/**

*privateintrow;

privateintcol;

privateStringvalue;

*

*/

Listlist=newArrayList();

JsonBean2jb1=newJsonBean2();

jb1.setCol(1);

jb1.setRow(1);

jb1.setValue("xx");

JsonBean2jb2=newJsonBean2();

jb2.setCol(2);

jb2.setRow(2);

jb2.setValue("");

list.add(jb1);

list.add(jb2);

JSONArrayja=JSONArray.fromObject(list);

System.out.println(ja.toString());

//[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]

5、Stringtobean

Java代码

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

JSONObjectjsonObject=JSONObject.fromString(json);

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

Listexpected=JSONArray.toList(jsonObject.getJSONArray("array"));

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

Java代码

Stringjson="{\"value\":\"xx\",\"row\":1,\"col\":1}";

JSONObjectjsonObject=JSONObject.fromString(json);

JsonBean2bean=(JsonBean2)JSONObject.toBean(jsonObject,JsonBean2.class);

assertEquals(jsonObject.get("col"),newInteger(bean.getCol()));

assertEquals(jsonObject.get("row"),newInteger(bean.getRow()));

assertEquals(jsonObject.get("value"),bean.getValue());

6jsontoxml

1)

JSONObjectjson=newJSONObject(true);

Stringxml=XMLSerializer.write(json);

2)

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

Stringxml=XMLSerializer.write(json);

json

true

1

json

true

1

3)

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

Stringxml=XMLSerializer.write(json);

1

2

3

7、xmltojson

returnmatrix[i][j];

returnmatrix[i][j];

JSONArrayjson=(JSONArray)XMLSerializer.read(xml);

System.out.println(json);

//prints[function(i,j){returnmatrix[i][j];}]

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值