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 + "]";
        }  
}



阅读更多
文章标签: json
个人分类: Java Android
上一篇json封装数据
下一篇Myeclipse 2015 stable 2.0 完美破解方法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭