json <--->List集合,实体类 之间的相互转换

json所依赖的jar包
https://files.cnblogs.com/files/wenjie123/json_jar%E5%8C%85.rar

package
com.hp.svse; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; /** * Timestamp 处理器 */ public class JsonDateValueProcessor implements JsonValueProcessor { private String formatDateTime ="yyyy-MM-dd HH:mm:ss"; private String formatDate ="yyyy-MM-dd"; public Object processArrayValue(Object value, JsonConfig config) { return process(value); } public Object processObjectValue(String key, Object value, JsonConfig config) { return process(value); } private Object process(Object value){ if(value instanceof Date){ SimpleDateFormat sdf ; if(value.toString().length()<=11||value.toString().indexOf("00:00:00.0")>=0){ sdf= new SimpleDateFormat(formatDate,Locale.UK); }else{ sdf= new SimpleDateFormat(formatDateTime,Locale.UK); } return sdf.format(value); } return value == null ? "" : value.toString(); } }
package com.hp.svse;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class JsonTest {
    public static void main(String[] args) {
        
        /**
         * 将对象集合转换成JSON格式的字符串
         */
        List<Student> students = new ArrayList<Student>();
        students.add(new Student("小明1", "男", "湖北", "SVSE"));
        students.add(new Student("小明2", "女", "广东", "GIS"));
        students.add(new Student("小明3", "男", "香港", "3G"));
        JsonTest jsonTest = new JsonTest();
        String json = jsonTest.beanListToJSON(students);
        System.out.println(json);
        /**
         * 输出结果:
         * [
         *     {"sex":"男","address":"湖北","stuname":"小明1","classname":"SVSE","smallStudents":[]},
         *     {"sex":"女","address":"广东","stuname":"小明2","classname":"GIS","smallStudents":[]},
         *     {"sex":"男","address":"香港","stuname":"小明3","classname":"3G","smallStudents":[]}
         * ]
         */
        
        
        
        
        
        /**
         * 将json字符串转换成json对象  再讲json对象转换成实体对象bean
         */
        String bookCotentjsonStr = "{\"sex\":\"男\",\"address\":\"湖北\",\"stuname\":\"小明1\",\"classname\":\"SVSE\"}";
        JSONObject bookCotentjson = JSONObject.fromObject(bookCotentjsonStr);
        Student student = (Student) JSONObject.toBean(bookCotentjson, Student.class);
        System.out.println(student);
        /**
         * 输出结果:Student [address=湖北, classname=SVSE, sex=男, stuname=小明1]
         */
        
        
        
        
        /**
         * 将对象转换成JSON格式的字符串 适用于单个对象(单个对象里可以包含List<T>)的转换
         */
        List<SmallStudent> smallStudents = new ArrayList<SmallStudent>();
        for (int i = 0; i < 3; i++) {
            SmallStudent smallStudent = new SmallStudent();
            smallStudent.setName("小学生"+i);
            smallStudent.setAge("1"+i);
            smallStudents.add(smallStudent);
        }
        Student student2 = new Student("小明", "男", "武汉", "svse",smallStudents);
        String jsonStr = jsonTest.beanToJSON(student2);
        System.out.println(jsonStr);
        /**
         * 输出结果:
         * {"sex":"男","address":"武汉","stuname":"小明","classname":"svse","smallStudents":[{"sex":"","age":"10","name":"小学生0"},{"sex":"","age":"11","name":"小学生1"},{"sex":"","age":"12","name":"小学生2"}]}
         */
        
        
        
    }
    /**
     * 将集合转换成JSON格式的字符串
     */
    public <T> String beanListToJSON(List<T> t) {
        String json = "";
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class,
                new JsonDateValueProcessor());
        if(t!=null){
        json = JSONArray.fromObject(t, jsonConfig).toString();
        }else{
            json="[]";
        }
        return json;
    }
    
    /**
     * 将对象转换成JSON格式的字符串 适用于单个对象(单个对象里可以包含List<T>)的转换
     */
    public <T> String beanToJSON(T t) {
        String json = "";
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(Date.class,
                new JsonDateValueProcessor());
        if(t!=null){
        json = JSONObject.fromObject(t, jsonConfig).toString();
        }else{
            json="[]";
        }
        return json;
    }
}
package com.hp.svse;

import java.util.List;

public class Student {
    private String stuname;
    private String sex;
    private String address;
    private String classname;
    private List<SmallStudent> smallStudents;
    
    public Student(String stuname, String sex, String address, String classname,List<SmallStudent> smallStudents) {
        super();
        this.stuname = stuname;
        this.sex = sex;
        this.address = address;
        this.classname = classname;
        this.smallStudents = smallStudents;
    }
    public Student(String stuname, String sex, String address, String classname) {
        super();
        this.stuname = stuname;
        this.sex = sex;
        this.address = address;
        this.classname = classname;
    }
    public Student() {
        super();
    }

    public String getStuname() {
        return stuname;
    }
    public void setStuname(String stuname) {
        this.stuname = stuname;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getClassname() {
        return classname;
    }
    public void setClassname(String classname) {
        this.classname = classname;
    }

    public List<SmallStudent> getSmallStudents() {
        return smallStudents;
    }

    public void setSmallStudents(List<SmallStudent> smallStudents) {
        this.smallStudents = smallStudents;
    }

    @Override
    public String toString() {
        return "Student [address=" + address + ", classname=" + classname
                + ", sex=" + sex + ", stuname=" + stuname + "]";
    }
    
}
package com.hp.svse;

public class SmallStudent {
    private String name;
    private String age;
    private String sex;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "SmallStudent [age=" + age + ", name=" + name + ", sex=" + sex
                + "]";
    }
}

 

posted @ 2015-07-13 16:44  闻杰  阅读(12414)  评论(1编辑  收藏  举报