java对象与json对象之间互相转换实现方法示例
本文实例讲述了java对象与json对象之间互相转换实现方法。分享给大家供大家参考,具体如下:
importjava.util.ArrayList;
importjava.util.Collection;
importjava.util.Iterator;
importjava.util.List;
importnet.sf.json.JSONArray;
importnet.sf.json.JSONObject;
publicclassMainClass{
publicstaticvoidmain(String[]args){
TestJsonBean();
TestJsonAttribute();
TestJsonArray();
}
@SuppressWarnings("rawtypes")
privatestaticvoidTestJsonArray(){
Studentstudent1=newStudent();
student1.setId(1);
student1.setName("jag");
student1.setSex("man");
student1.setAge(25);
student1.setHobby(newString[]{"篮球","游戏"});
Studentstudent2=newStudent();
student2.setId(2);
student2.setName("tom");
student2.setSex("woman");
student2.setAge(23);
student2.setHobby(newString[]{"上网","跑步"});
Listlist=newArrayList();
list.add(student1);
list.add(student2);
JSONArrayjsonArray=JSONArray.fromObject(list);
System.out.println(jsonArray.toString());
JSONArraynew_jsonArray=JSONArray.fromObject(jsonArray.toArray());
Collectionjava_collection=JSONArray.toCollection(new_jsonArray);
if(java_collection!=null&&!java_collection.isEmpty())
{
Iteratorit=java_collection.iterator();
while(it.hasNext())
{
JSONObjectjsonObj=JSONObject.fromObject(it.next());
Studentstu=(Student)JSONObject.toBean(jsonObj,Student.class);
System.out.println(stu.getName());
}
}
}
privatestaticvoidTestJsonAttribute(){
/**
*创建json对象并为该对象设置属性
*/
JSONObjectjsonObj=newJSONObject();
jsonObj.put("Int_att",25);//添加int型属性
jsonObj.put("String_att","str");//添加string型属性
jsonObj.put("Double_att",12.25);//添加double型属性
jsonObj.put("Boolean_att",true);//添加boolean型属性
//添加JSONObject型属性
JSONObjectjsonObjSon=newJSONObject();
jsonObjSon.put("id",1);
jsonObjSon.put("name","tom");
jsonObj.put("JSONObject_att",jsonObjSon);
//添加JSONArray型属性
JSONArrayjsonArray=newJSONArray();
jsonArray.add("array0");
jsonArray.add("array1");
jsonArray.add("array2");
jsonArray.add("array3");
jsonObj.put("JSONArray_att",jsonArray);
System.out.println(jsonObj.toString());
System.out.println("Int_att:"+jsonObj.getInt("Int_att"));
System.out.println("String_att:"+jsonObj.getString("String_att"));
System.out.println("Double_att:"+jsonObj.getDouble("Double_att"));
System.out.println("Boolean_att:"+jsonObj.getBoolean("Boolean_att"));
System.out.println("JSONObject_att:"+jsonObj.getJSONObject("JSONObject_att"));
System.out.println("JSONArray_att:"+jsonObj.getJSONArray("JSONArray_att"));
}
/**
*java对象与json对象互相转换
*/
privatestaticvoidTestJsonBean(){
/**
*创建java对象
*/
Studentstudent=newStudent();
student.setId(1);
student.setName("jag");
student.setSex("man");
student.setAge(25);
student.setHobby(newString[]{"篮球","上网","跑步","游戏"});
/**
*java对象转换成json对象,并获取json对象属性
*/
JSONObjectjsonStu=JSONObject.fromObject(student);
System.out.println(jsonStu.toString());
System.out.println(jsonStu.getJSONArray("hobby"));
/**
*json对象转换成java对象,并获取java对象属性
*/
Studentstu=(Student)JSONObject.toBean(jsonStu,Student.class);
System.out.println(stu.getName());
/**
*创建json对象
*/
JSONObjectjsonObj=newJSONObject();
jsonObj.put("id",1);
jsonObj.put("name","张勇");
jsonObj.put("sex","男");
jsonObj.put("age",24);
//jsonObj.put("hobby",newString[]{"上网","游戏","跑步","音乐"});
//System.out.println(jsonObj.toString());
/**
*json对象转换成java对象
*/
Studentstud=(Student)JSONObject.toBean(jsonObj,Student.class);
System.out.println(stud.getName());
}
}
PS:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:
在线
JSON在线格式化工具:
http://tools.jb51.net/code/jsonformat
在线XML/