本文目录一览:
- 1、java对象怎么转json数组 跪求啊
- 2、java怎么把object转成json
- 3、java中object数据怎么转换成json数据
- 4、java 怎么将List类型转换成json类型,怎么读取json类型
- 5、如何用java实现list转换成json格式
java对象怎么转json数组 跪求啊
JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。
下载地址:
还要需要的第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};
JSONArray jsonArray1 = JSONArray.fromObject( boolArray );
System.out.println( jsonArray1 );
// prints [true,false,true]
List list =newArrayList();
list.add("first");
list.add("second");
JSONArray jsonArray2 = JSONArray.fromObject( list );
System.out.println( jsonArray2 );
// prints ["first","second"]
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']");
System.out.println( jsonArray3 );
// prints ["json","is","easy"]
2、Map
Java代码
Map map =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){ return this.arr[i]; }");
JSONObject json = JSONObject.fromObject( map );
System.out.println( json );
//{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}
3、BEAN
Java代码
/**
* Bean.java
private String name = "json";
private int pojoId = 1;
private char[] ptions = new char[]{'a','f'};
private String func1 = "function(i){ return this.options[i]; }";
private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
*/
JSONObject jsonObject = JSONObject.fromObject(newJsonBean() );
System.out.println( jsonObject );
//{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}
4、BEANS
Java代码
/**
* private int row ;
private int col ;
private String value ;
*
*/
List list =newArrayList();
JsonBean2 jb1 =newJsonBean2();
jb1.setCol(1);
jb1.setRow(1);
jb1.setValue("xx");
JsonBean2 jb2 =newJsonBean2();
jb2.setCol(2);
jb2.setRow(2);
jb2.setValue("");
list.add(jb1);
list.add(jb2);
JSONArray ja = JSONArray.fromObject(list);
System.out.println( ja.toString() );
//[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]
5、String to bean
Java代码
String json ="{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
JSONObject jsonObject = JSONObject.fromString(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") );
assertEquals( expected, (List) PropertyUtils.getProperty( bean,"array") );
Java代码
String json ="{\"value\":\"xx\",\"row\":1,\"col\":1}";
JSONObject jsonObject = JSONObject.fromString(json);
JsonBean2 bean = (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() );
6 json to xml
1)
JSONObject json = new JSONObject( true );
String xml = XMLSerializer.write( json );
o class="object" null="true"
2)
JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");
String xml = XMLSerializer.write( json );
o class="object"
name type="string"json/name
bool type="boolean"true/bool
int type="number"1/int
/o
o class="object"
name type="string"json/name
bool type="boolean"true/bool
int type="number"1/int
/o
3)
JSONArray json = JSONArray.fromObject("[1,2,3]");
String xml = XMLSerializer.write( json );
a class="array"
e type="number"1/e
e type="number"2/e
e type="number"3/e
/a
7 、xml to json
a class="array"
e type="function" params="i,j"
return matrix[i][j];
/e
/a
a class="array"
e type="function" params="i,j"
return matrix[i][j];
/e
/a
JSONArray json = (JSONArray) XMLSerializer.read( xml );
System.out.println( json );
// prints [function(i,j){ return matrix[i][j]; }]
java怎么把object转成json
1、json-lib是一个java类库,提供将Java对象,包括beans, maps, collections, java arrays and XML等转换成JSON,或者反向转换的功能。
java中object数据怎么转换成json数据
你可以通过这个(json-lib-2.3-jdk15.jar)jar里的方法转换
JSONObject json = JSONObject.fromObject(Object);
如果对象数组
JSONArray json = JSONArray .fromObject(person);
java 怎么将List类型转换成json类型,怎么读取json类型
java中将list对象转换成json类型,可以使用json拼接的方式,实例如下:
json:[{},{}]
String json="[";
for(Object obj:list){
json=json+"{"+obj+"}";
}
json=json+"]"
return json;
如何用java实现list转换成json格式
首先要导入json包
import net.sf.json.JSONObject;
import net.sf.json.JSON;
list转成json数组:
JSONArray arr = JSONArray.formObject(cc);
再讲json数组放入json对象:
JSONObject json = new JSONObject();
json.put("obj",arr);
要下载第三方的jar包,你自己找找啊