Excel转JSON怎么转

2025-02-26 04:26:29
推荐回答(4个)
回答1:

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

下载地址:http://json-lib.sourceforge.net/
还要需要的第3方包:
org.apache.commons(3.2以上版本)
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];}]

回答2:

http://tool.chinaz.com/tools/excel2json.aspx 可以在线转,也可以下载转化工具

回答3:

用jsonobject有个Jsonobject.from(object);的方法

回答4:

用vba来实现