Fastjson中bean转json和json转bean

陆耀
2023-12-01
public class Sign {
    private String sno;//	学号
    private String sname;//	学生名
    private String signtime;//	签到时间
    private String coursename;//		课程名
    private String teachername;//授课教师名
    private String coursetime;//这门课程的上课时间
    public Sign(String sno, String sname, String signtime, String coursename, 
    String teachername, String coursetime) {
        this.sno = sno;
        this.sname = sname;
        this.signtime = signtime;
        this.coursename = coursename;
        this.teachername = teachername;
        this.coursetime = coursetime;
    }
    @Override
    public String toString() {
        return "Sign{" +
                "sno='" + sno + '\'' +
                ", sname='" + sname + '\'' +
                ", signtime='" + signtime + '\'' +
                ", coursename='" + coursename + '\'' +
                ", teachername='" + teachername + '\'' + '\'' +
                ", coursetime='" + coursetime + '\'' 
                '}';
    }
}

 

JsonString转bean

/**
   * 将json对象转为bean对象
   * @param product
   */
  private static void jsonToBean2(String product) {
    JSONObject jsonObject = JSON.parseObject(product);
    Sign sign = JSON.parseObject(jsonObject.toJSONString(), new TypeReference<Sign>(){});
    System.out.println(sign);
  }

bean对象转json

    sign.setSno(sno);
    sign.setCoursename(courseName);
    sign.setCoursetime(courseTime);
    sign.setSigntime(signtime);
    sign.setSname(sName);
    sign.setTeachername(teacherName);
    String signString= JSON.toJSONString(sign);

普通的jsonstring转json

String str = "{\"语文\":\"88\",\"数学\":\"78\",\"计算机\":\"99\"}"; 
JSONObject jsonObject; 
jsonObject = JSONObject.parseObject(str); 

 

 类似资料: