fastjson快速入门

public class User {
    private Integer id;
    private String name;
    private String address;
    private Date birth;
    public User() {
    }
    public User(Integer id, String name, String address, Date birth) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.birth = birth;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
}
public class Test01FastJson {
    public static void main(String[] args) {
        User user=new User(1,"小明","武汉",new Date());
        //对象 -> JOSN字符串
        System.out.println(JSON.toJSONString(user));

        //对象 -> JSONObject
        //转化成JSON对象  本质也就是一个Map
        JSONObject jsonObject = (JSONObject)JSON.toJSON(user);
        System.out.println(jsonObject.get("id"));
        System.out.println(jsonObject.get("name"));
        System.out.println(jsonObject.get("address"));
        System.out.println(jsonObject.get("birth"));

    }
}

 

public class Test02FastJson {
    public static void main(String[] args) {
        List<User> users=new ArrayList<>();
        for (int i = 1; i <=5 ; i++) {
            users.add(new User(i,"小明"+i,"武汉"+i,new Date()));
        }
        //list对象 -> JSON字符串
        String s = JSON.toJSONString(users);
        System.out.println(s);

        //list对象 -> JSONArray
        //转化成JSON对象  本质也就是一个Map
        JSONArray array = (JSONArray)JSON.toJSON(users);
        for (Object o : array) {
            if (o instanceof JSONObject){
                JSONObject o1 = (JSONObject) o;
                System.out.println(o1.get("id")+" "+o1.get("name")+" "+o1.get("address")+" "+o1.get("birth"));
            }

        }
    }
}

 

public class Test03FastJson {
    public static void main(String[] args) {
        String json="{\"address\":\"武汉\",\"birth\":1609943410183,\"id\":1,\"name\":\"小明\"}";

        //json字符串 -> JSONObject
        JSONObject jsonObject = (JSONObject)JSON.parse(json);
        System.out.println(jsonObject);
        System.out.println(jsonObject.get("id"));
        System.out.println(jsonObject.get("address"));
        System.out.println(jsonObject.get("name"));
        System.out.println(jsonObject.get("birth"));

        //json字符串 -> 对象
        User user = JSON.parseObject(json, User.class);
        System.out.println(user.getId()+" "+user.getName()+"  "+user.getAddress()+"  "+user.getBirth());
    }
}

 

public class Test04FastJson {
    public static void main(String[] args) {
        String json="[{\"address\":\"武汉1\",\"birth\":1609943580570,\"id\":1,\"name\":\"小明1\"},{\"address\":\"武汉2\",\"birth\":1609943580570,\"id\":2,\"name\":\"小明2\"},{\"address\":\"武汉3\",\"birth\":1609943580570,\"id\":3,\"name\":\"小明3\"},{\"address\":\"武汉4\",\"birth\":1609943580570,\"id\":4,\"name\":\"小明4\"},{\"address\":\"武汉5\",\"birth\":1609943580570,\"id\":5,\"name\":\"小明5\"}]";
        //JSON字符串 -> JSONArray
        JSONArray array = (JSONArray)JSON.parse(json);
        for (Object o : array) {
            if (o instanceof JSONObject){
                JSONObject o1 = (JSONObject) o;
                System.out.println(o1.get("id")+" "+o1.get("name")+" "+o1.get("address")+" "+o1.get("birth"));
            }
        }
        System.out.println();
        //JSON字符串 -> list
        List<User> users = JSON.parseArray(json, User.class);
        for (User user : users) {
            System.out.println(user.getId()+" "+user.getName()+"  "+user.getAddress()+"  "+user.getBirth());
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值