使用JSONObject将实体类,Json字符串和Json类型相互转换(java)

使用的是com.alibaba.fastjson.JSONObject

JSONObject的maven依赖:

      
      <dependency>
          <groupId>com.alibabagroupId>
          <artifactId>fastjsonartifactId>
          <version>1.2.58version>
      dependency>

People.java

package com.dddd.entity;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.io.Serializable;
import java.util.Date;

/**
 * @ClassName People
 * @Description
 * @Date 2019/10/18
 * @Version 1.0
 */
public class People implements Serializable{

    private static final long serialVersionUID = -874758492035405289L;

    /**
     * @Description id
     * @Date 2019/10/18 15:46
     * @Param
     * @return
     **/
    private int id;

    /**
     * @Description 姓名
     * @Date 2019/10/18 15:46
     * @Param
     * @return
     **/
    private String name;

    /**
     * @Description 性别
     * @Date 2019/10/18 15:46
     * @Param
     * @return
     **/
    private String sex;

    /**
     * @Description 年龄
     * @Date 2019/10/18 15:46
     * @Param
     * @return
     **/
    private int age;

    /**
     * @Description 出生时间
     * @Date 2019/10/18 15:46
     * @Param
     * @return
     **/
    private Date birthday;

    public int getId() {
        return id;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex == null ? null : sex.trim();
    }

    @Override
    public String toString() {
        return "People{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
}

JSONObjectTest.java测试类

package com.dddd;

import com.alibaba.fastjson.JSONObject;
import com.dddd.entity.People;

import java.util.Date;

/**
 * @ClassName JSONObjectTest
 * @Description
 * @Date 2019/10/18
 */
public class JSONObjectTest {

    public static void main(String[] args) {
        //创建JSON对象
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", 1);
        jsonObject.put("age", 1);
        jsonObject.put("name", "明明");
        jsonObject.put("sex", "男");
        jsonObject.put("birthday", new Date());

        //将JSON格式转换为Object
        People jsonToObject = JSONObject.toJavaObject(jsonObject, People.class);
        System.out.println("将JSON转换为Object:");
        System.out.println(jsonToObject.toString() + "\n");

        //将Object转换为JSON字符串
        String objectToStr = JSONObject.toJSONString(jsonToObject);
        System.out.println("将Object转换为JSON字符串:");
        System.out.println(objectToStr + "\n");

        //将JSON字符串转换为JSON格式
        JSONObject strToJson = JSONObject.parseObject(objectToStr);
        //通过JSONObject获取属性
        int id = (int)strToJson.get("id");
        int age = (int)strToJson.get("age");
        String name = (String)strToJson.get("name");
        String sex = (String)strToJson.get("sex");
        Date birthday = new Date((long) strToJson.get("birthday"));
        System.out.println("字符串转换为JSON格式:");
        System.out.println("通过JSON对象获取到的属性:id:" + id + ",age:" + age + ",name:" + name + ",sex:" + sex + "," + birthday);
    }
}

控制台打印如下:

将JSON转换为Object:
People{id=1, name='明明', sex='男', age=1, birthday=Fri Oct 18 16:48:30 CST 2019}

将Object转换为JSON字符串:
{"age":1,"birthday":1571388510371,"id":1,"name":"明明","sex":"男"}

字符串转换为JSON格式:
通过JSON对象获取到的属性:id:1,age:1,name:明明,sex:男,Fri Oct 18 16:48:30 CST 2019

总结

  1. 将Object实体类转换为Json字符串,使用 JSONObject.toJSONString(Object obj)
  2. 将Json字符串转换为Json格式,使用JSONObject.parseObject(String str)
  3. 将Json格式转换为Object实体类,使用JSONObject.toJavaObject(JSONObject jsonObject, Object.class)

你可能感兴趣的:(java)