阿里的fastJSON的用法

新建两个实体User和Teacher

package org.fastjson;

public class User {
    private int id;

    private String userName;

    public User() {
    }


    public User(int id, String userName) {
        this.id = id;
        this.userName = userName;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    public String getUserName() {
        return userName;
    }


    public void setUserName(String userName) {
        this.userName = userName;
    }


    @Override
    public String toString() {
        return "User [id=" + id + ", userName=" + userName + "]";
    }


}
package org.fastjson;

public class Teacher {

    private int id;

    private String classNo;

    private String courseNo;

    public Teacher() {
        super();
    }

    public Teacher(int id, String classNo, String courseNo) {
        this.id = id;
        this.classNo = classNo;
        this.courseNo = courseNo;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getClassNo() {
        return classNo;
    }

    public void setClassNo(String classNo) {
        this.classNo = classNo;
    }

    public String getCourseNo() {
        return courseNo;
    }

    public void setCourseNo(String courseNo) {
        this.courseNo = courseNo;
    }

    @Override
    public String toString() {
        return "Teacher [id=" + id + ", classNo=" + classNo + ", courseNo=" + courseNo + "]";
    }

}
package org.fastjson;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

public class FastjsonClass {

    public static void main(String[] args) {

        System.out.println(mapToJson());

    }

    public void parseJson(String jsonStr){
        User user = new User(11,"京津冀");
        Teacher teacher = new Teacher(1,"A1000","CC2000");
        Teacher teacher1 = new Teacher(2,"A1001","CC2001");

        List<Teacher> teacherList = new ArrayList<Teacher>();
        teacherList.add(teacher);
        teacherList.add(teacher1);
        Temp temp = new Temp(user,teacherList);

        String tempJson = JSON.toJSONString(temp);  
        System.out.println(tempJson);
        JSONObject jsonObj = JSON.parseObject(tempJson);  
        JSONArray result = jsonObj.getJSONArray("teacherList");  
        List<Teacher> th = JSON.parseArray(result.toJSONString(),Teacher.class);  
        System.out.println(th.toString());

        JSONObject userObj = jsonObj.getJSONObject("user");

        User user1 = JSON.parseObject(userObj.toJSONString(), new TypeReference<User>() {});

        User user2 = JSON.parseObject(userObj.toJSONString(), User.class);
        System.out.println(user.toString());

        try {
            DButils.getConnection();
            // System.out.println(getCount("SELECT count(table_name) FROM information_schema.TABLES WHERE table_name='book' "));

            DButils.insertEntity(User.class, user2);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static String mapToJson(){

        Map<String, Object> map = new HashMap<String, Object>();
        User user = new User(11,"京津冀");
        map.put("user", user);

        Teacher teacher = new Teacher(1,"A1000","CC2000");
        Teacher teacher1 = new Teacher(2,"A1001","CC2001");
        List<Teacher> teacherList = new ArrayList<Teacher>();
        teacherList.add(teacher);
        teacherList.add(teacher1);
        map.put("teacherList", teacherList);
        return JSON.toJSONString(map); 
    }


}
发布了28 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/wodetongnian/article/details/78730108