java对象与json对象之间互相转换实现方法示例

  

本文实例讲述了java对象与json对象之间互相转换实现方法。分享给大家供大家参考,具体如下:

        进口java.util.ArrayList;   进口java.util.Collection;   进口java.util.Iterator;   进口并不知道;   进口net.sf.json.JSONArray;   进口net.sf.json.JSONObject;   公开课MainClass {   公共静态void main (String [] args) {   TestJsonBean ();   TestJsonAttribute ();   TestJsonArray ();   }   @SuppressWarnings (“rawtypes”)   私有静态孔隙TestJsonArray () {   学生学生1=new ();   student1.setId (1);   student1.setName(“缺口”);   student1.setSex(“男人”);   student1.setAge (25);   学生1。setHobby(新String[]{“篮球”,“游戏"});   学生学生二=new ();   student2.setId (2);   student2.setName(“汤姆”);   student2.setSex(“女人”);   student2.setAge (23);   学生二。setHobby(新String[]{”上网”,“跑步"});   List列表=new ArrayList ();   list.add(学生1);   list.add(学生二);   JSONArray JSONArray=JSONArray.fromObject(列表);   System.out.println (jsonArray.toString ());   JSONArray new_jsonArray=JSONArray.fromObject (jsonArray.toArray ());   收集java_collection=JSONArray.toCollection (new_jsonArray);   如果(java_collection !=零,,! java_collection.isEmpty ())   {   它=java_collection.iterator Iterator ();   而(it.hasNext ())   {   JSONObject jsonObj=JSONObject.fromObject (it.next ());   学生stu=(学生)JSONObject.toBean (jsonObj Student.class);   System.out.println (stu.getName ());   }   }   }   私有静态孔隙TestJsonAttribute () {/* *   *创建json对象并为该对象设置属性   */JSONObject jsonObj=new JSONObject ();   jsonObj.put (“Int_att”, 25);//添加int型属性   jsonObj.put (“String_att”、“str ");//添加字符串型属性   jsonObj.put (“Double_att”, 12.25);//添加双型属性   jsonObj.put (“Boolean_att”,真的);//添加布尔型属性//添加JSONObject型属性   JSONObject jsonObjSon=new JSONObject ();   jsonObjSon。put (" id ", 1);   jsonObjSon。把(“名字”,“汤姆”);   jsonObj.put (“JSONObject_att”, jsonObjSon);//添加JSONArray型属性   JSONArray JSONArray JSONArray=new ();   jsonArray.add (“array0”);   jsonArray.add (“array1”);   jsonArray.add (“array2”);   jsonArray.add (“array3”);   jsonObj。把(jsonArray JSONArray_att》);   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对象互相转换   */私有静态孔隙TestJsonBean () {/* *   *创建java对象   */学生学生=new ();   student.setId (1);   student.setName(“缺口”);   student.setSex(“男人”);   student.setAge (25);   学生。setHobby(新String[]{“篮球”,“上网”,“跑步”、“游戏"});/* *   * java对象转换成json对象,并获取json对象属性   */JSONObject jsonStu=JSONObject.fromObject(学生);   System.out.println (jsonStu.toString ());   System.out.println (jsonStu.getJSONArray("爱好"));/* *   * json对象转换成java对象,并获取java对象属性   */学生stu=(学生)JSONObject。toBean (jsonStu Student.class);   System.out.println (stu.getName ());/* *   *创建json对象   */JSONObject jsonObj=new JSONObject ();   jsonObj.put (" id ", 1);   jsonObj.put(“名字”、“张勇”);   jsonObj.put(“性”,“男”);   jsonObj.put(“年龄”,24);//jsonObj。put(“爱好”,新String[]{”上网”,“游戏”、“跑步”、“音乐"});//System.out.println (jsonObj.toString ());/* *   * json对象转换成java对象   */学生学生=(学生)JSONObject.toBean (jsonObj Student.class);   System.out.println (stud.getName ());   }   }      之前      

  

<强>代码检验,检验,美化,格式化工具:
   http://tools.jb51.net/code/json

  http://tools.jb51.net/code/jsonformat

  

互相转换工具:
   http://tools.jb51.net/code/xmljson

  http://tools.jb51.net/code/jsoncodeformat

java对象与json对象之间互相转换实现方法示例