java对象怎么转json数组 跪求啊 如何将java对象数组转化为JSON树
json\u6570\u7ec4\u8f6cjava\u5bf9\u8c61\u600e\u4e48\u8f6c\uff1f1\u3001\u9700\u8981json\u7684\u76f8\u5173jar\u5305
2\u3001\u8bbe\u7f6ejson\u683c\u5f0f\u5b57\u7b26\u4e32\uff1a
String str="{\"student\":[{\"name\":\"leilei\",\"age\":23},{\"name\":\"leilei02\",\"age\":23}]}";
3\u3001\u5efa\u7acb\u76f8\u5e94\u7684\u7c7b\uff1a
public class StudentList {
List student;
public List getStudent() {
return student;
}
public void setStudent(List student) {
this.student = student;
}
}
public class Student {
private String name;
private int age;
//private StudentClass studentClass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4\u3001\u6d4b\u8bd5\u7a0b\u5e8f
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;
public class JsonToJavaBean {
public static void main(String[] args) {
String str="{\"student\":[{\"name\":\"leilei\",\"age\":23},{\"name\":\"leilei02\",\"age\":23}]}";
Student stu = null;
List list = null;
try {
ObjectMapper objectMapper=new ObjectMapper();
StudentList studentList=objectMapper.readValue(str, StudentList.class);
list=studentList.getStudent();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(Student s:list){
System.out.println(s.getName());
}
}
//\u63a5\u53d7\u4e00\u4e2aListnet.sf.json.JSONArray.fromObject(dataList);//\u63a5\u53d7\u4e00\u4e2abeannet.sf.json.JSONObject.fromObject(Object object);\u5229\u7528json-lib\uff0c\u5c01\u88c5\u597d\u5bf9\u8c61\u7684\u5c42\u7ea7\u5173\u7cfb\u540e\uff0c\u76f4\u63a5\u53ef\u4ee5\u7528\u4e0a\u9762\u4e24\u4e2a\u65b9\u6cd5\u6765\u8f6c\u6362json\u683c\u5f0f\u6570\u636e\u3002
下载地址: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};
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对象中的数据保存在一个json字符串中,在通过js中eval 方法编译json字符串成相应的对象。
绛旓細:"pointkkkk","type":3},{"description":"descrsssss","id":2,"pointInfo":"pointkkkk","type":3}]椤甸潰 .post("showQuestions?type="+type,null,callbackShowQuestions,"json");杩欐椂鍦ㄥ洖璋冨嚱鏁扮殑鍙傛暟data鑾峰緱鐨勫氨鏄json杞崲鍚庣殑js瀵硅薄鏁扮粍锛屼互涓嬫爣鏂瑰紡渚垮彲璁块棶鏁扮粍閲岀殑鐩稿簲瀵硅薄淇℃伅銆
绛旓細棣栧厛寮曠敤json 瀵瑰簲鐨刯ar鍖 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar 5.ezmorph.jar 6.json-lib-2.2.2-jdk15.jar 瀹炵幇鏂规硶灏辨槸 JSONObjectjson = JSONObject.fromObject(map);
绛旓細List list = new ArraryList();list.add(employee); //娣诲姞绫籩mployee list.add(employer); //娣诲姞绫籩mployer jsonArray = JSONArray.fromObject(list); //杞崲涓簀son鏁扮粍 杩欐牱灏辫浜
绛旓細闇瑕佸鍏ヤ袱涓猨ar鍖 json-lib鏄敤浜庤浆鎹son瀛楃涓茬殑鏍稿績jar鍖咃紝涓婇潰閭d釜鏄緟鍔╃殑銆杞崲json鏁扮粍灏辨槸JSONArray.fromObject(arrayList).toString();杞崲json瀵硅薄灏辨槸JSONObject.fromObject(arrayList).toString();
绛旓細棣栧厛锛孞SON搴忓垪鍖栨槸灏Java瀵硅薄杞崲鎴怞SON瀛楃涓茬殑杩囩▼銆備緥濡傦紝褰撲竴涓狫ava绫伙紝濡俇ser锛屽寘鍚祵濂楀璞℃垨闆嗗悎锛孞ackson鐨凮bjectMapper绫荤殑writeValueAsString鏂规硶浼氳嚜鍔ㄥ皢杩欎簺灞炴ц浆鎹负閿煎锛屽舰鎴怞SON瀵硅薄鍜鏁扮粍缁撴瀯銆傚弽搴忓垪鍖栧垯鏄粠JSON瀛楃涓叉仮澶岼ava瀵硅薄銆侸ackson鐨凮bjectMapper绫荤殑readValue鏂规硶鎺ユ敹JSON瀛楃涓插拰...
绛旓細瀵煎叆Google鐨勫寘gson-2.2.4.jar 鐒跺悗瀹炰緥鍖朑son static Gson gosn = new Gson();String json = gosn.toJson(hashMap); //杩欓噷鏀句竴涓瀵硅薄锛屼粈涔堝璞¢兘鍙互銆杞寲鍚庡氨鏄疛son锛屽姛鑳藉己澶у緢澶氾紝涔熺畝鍗曞緢澶氥俲son-lib-2.4-jdk15.jar ezmorph-1.0.6.jar 杞崲鐨勮瘽杩欐牱鐢 String s= JSONArray....
绛旓細// 闇瑕佺敤鍒癹son-lib.jar锛屽彲浠ュ埌杩欓噷涓嬭浇锛歨ttp://json-lib.sourceforge.net/ // 浠g爜瀹炰緥 List<String> list = new ArrayList<String>();list.add("abc");list.add("123");net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(list);System.out.println(jsonArray....
绛旓細鎴戠殑浠g爜濡備笅锛歱rivate Timestamp time; public Timestamp getTime() { return time; } public void setTime(Timestamp time) { this.time = time; }娴嬭瘯鏂规硶锛歱ublic static void main(String[] args) throws JsonProcessingException, ParseException { Timestamp timestamp = ...
绛旓細1銆佷粠涓涓狫SON 瀵硅薄瀛楃鏍煎紡涓緱鍒颁竴涓java瀵硅薄 璇风湅涓嬪浘锛屽涓嬪浘鎵绀 2銆佷粠涓涓狫SON 瀵硅薄瀛楃鏍煎紡涓緱鍒颁竴涓猨ava瀵硅薄 甯︿笂鎸囧畾鐨勬棩鏈熸牸寮 璇风湅涓嬪浘锛屽涓嬪浘鎵绀 3銆佷粠json HASH琛ㄨ揪寮忎腑鑾峰彇涓涓猰ap 璇风湅涓嬪浘锛屽涓嬪浘鎵绀 4銆佷粠json鏁扮粍涓緱鍒扮浉搴java鏁扮粍 璇风湅涓嬪浘锛屽涓嬪浘鎵绀 5銆佷粠json鏁扮粍...
绛旓細2. JSON鏍煎紡鍒獼S瀵硅薄璇硶琛ㄧ幇涓婁弗鏍煎湪鍝 鍏堝氨浠モ滈敭鍊煎涓鸿〃鐜扮殑瀵硅薄鈥濆舰寮忎笂锛屽姣斾笅涓よ呯殑涓嶅悓锛岃嚦浜嶫SON杩樿兘浠鎬庢牱鐨勫舰寮忚〃鐜帮紝瀵规瘮瀹屽悗鍐嶇綏鍒椼傚彲浠ョ湅鍒帮紝鐩稿浜嶫S瀵硅薄锛孞SON鐨勬牸寮忔洿涓ユ牸锛屾墍浠ュぇ閮ㄥ垎鍐欑殑JS瀵硅薄鏄笉绗﹀悎JSON鐨勬牸寮忕殑銆俲son瀛楃涓杞崲鎴恓son鏁扮粍骞堕亶鍘嗗睘鎬у兼柟寮忎竴锛氫娇鐢↖terator杩唬...