阿里的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); 
    }


}
阿里巴巴 fastjson 是一个 Java 序列化/反序列化库,可以将 Java 对象转换为 JSON 格式的字符串,也可以将 JSON 格式的字符串转换为 Java 对象。以下是 fastjson 的基本用法: 1. 将 Java 对象转换为 JSON 字符串 ```java import com.alibaba.fastjson.JSON; public class FastJsonDemo { public static void main(String[] args) { User user = new User("Tom", 20); String jsonString = JSON.toJSONString(user); System.out.println(jsonString); } } class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } } ``` 输出结果: ``` {"age":20,"name":"Tom"} ``` 2. 将 JSON 字符串转换为 Java 对象 ```java import com.alibaba.fastjson.JSON; public class FastJsonDemo { public static void main(String[] args) { String jsonString = "{\"age\":20,\"name\":\"Tom\"}"; User user = JSON.parseObject(jsonString, User.class); System.out.println(user.getName() + ", " + user.getAge()); } } class User { private String name; private int age; public User() {} public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } ``` 输出结果: ``` Tom, 20 ``` 除此之外,fastjson 还提供了很多其他的用法,比如序列化/反序列化泛型类型、处理日期类型、处理循环引用等等。如果你想了解更多 fastjson用法,可以参考官方文档:https://github.com/alibaba/fastjson/wiki/Quick-Start-CN
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值