1. 程式人生 > >json:實體類和JSON物件之間相互轉化

json:實體類和JSON物件之間相互轉化

在需要用到JSON物件封裝資料的時候,往往會寫很多程式碼,也有很多複製貼上,為了用POJO的思想我們可以裝JSON轉化為實體物件進行操作
1. [程式碼]工具類     
packagemyUtil;
 
importjava.io.IOException;
 
importmyProject.Student;
importmyProject.StudentList;
 
importorg.codehaus.jackson.map.ObjectMapper;
importorg.json.JSONArray;
importorg.json.JSONException;
importorg.json.JSONObject;
/**
 * 實體類和JSON物件之間相互轉化(依賴包jackson-all-1.7.6.jar、jsoup-1.5.2.jar)
 * @author wck
 *
 */
publicclass JSONUtil {
    /**
     * 將json轉化為實體POJO
     * @param jsonStr
     * @param obj
     * @return
     */
    publicstatic<T> Object JSONToObj(String jsonStr,Class<T> obj) {
        T t = null;
        try{
            ObjectMapper objectMapper = newObjectMapper();
            t = objectMapper.readValue(jsonStr,
                    obj);
        }catch(Exception e) {
            e.printStackTrace();
        }
        returnt;
    }
     
    /**
     * 將實體POJO轉化為JSON
     * @param obj
     * @return
     * @throws JSONException
     * @throws IOException
     */
    publicstatic<T> JSONObject objectToJson(T obj) throwsJSONException, IOException {
        ObjectMapper mapper = newObjectMapper();  
        // Convert object to JSON string  
        String jsonStr = "";
        try{
             jsonStr =  mapper.writeValueAsString(obj);
        }catch(IOException e) {
            throwe;
        }
        returnnew JSONObject(jsonStr);
    }
     
     
    publicstatic void main(String[] args) throwsJSONException, IOException {
        JSONObject obj = null;
        obj = newJSONObject();
        obj.put("name","213");
        obj.put("age",27);
        JSONArray array = newJSONArray();
        array.put(obj);
        obj = newJSONObject();
        obj.put("name","214");
        obj.put("age",28);
        array.put(obj);
        Student stu = (Student) JSONToObj(obj.toString(), Student.class);
        JSONObject objList = newJSONObject();
        objList.put("student", array);
        System.out.println("objList:"+objList);
        StudentList stuList = (StudentList) JSONToObj(objList.toString(), StudentList.class);
        System.out.println("student:"+stu);
        System.out.println("stuList:"+stuList);
        System.out.println("#####################################");
        JSONObject getObj = objectToJson(stu);
        System.out.println(getObj);
    }
}

2. [程式碼]實體(子類)     
packagemyProject;
 
publicclass Student {
     privateString name;
        privateint age;
        //private StudentClass studentClass;
        publicString getName() {
            returnname;
        }
        publicvoid setName(String name) {
            this.name = name;
        }
        publicint getAge() {
            returnage;
        }
        publicvoid setAge(intage) {
            this.age = age;
        }
        @Override
        publicString toString() {
            return"Student [name=" + name + ", age=" + age + "]";
        }
}
3. [程式碼]實體父類     
packagemyProject;
 
importjava.util.List;
 
publicclass StudentList {
     List<Student> student;
        publicList<Student> getStudent() {
            returnstudent;
        }
      
        publicvoid setStudent(List<Student> student) {
            this.student = student;
        }
 
        @Override
        publicString toString() {
            return"StudentList [student=" + student + "]";
        }  
}