1.基本使用说明

FastJson常用到一下三个类:

(1)JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换。

(2)JSONObject:fastJson提供的json对象。

(3)JSONArray:fastJson提供json数组对象。

2.案例

public class Student {private String id;@JSONField(name="myName")  //自定义字段名字private String name;@JSONField(serialize=false)  //是否加入到序列化和反序列化中,true为加入,false为不加入,默认为trueprivate Integer age;@JSONField(format="yyyy-MM-dd hh:mm:ss")  //格式化时间字段private Date birthday;private List<Teacher> teacherList;private Teacher teacher;private Map<String, String> data;private List<Map<String, String>> dataList;private Integer sex;private Float weight;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public List<Teacher> getTeacherList() {return teacherList;}public void setTeacherList(List<Teacher> teacherList) {this.teacherList = teacherList;}public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}public Map<String, String> getData() {return data;}public void setData(Map<String, String> map) {this.data = map;}public Integer getSex() {return sex;}public void setSex(Integer sex) {this.sex = sex;}public Float getWeight() {return weight;}public void setWeight(Float weight) {this.weight = weight;}public List<Map<String, String>> getDataList() {return dataList;}public void setDataList(List<Map<String, String>> dataList) {this.dataList = dataList;}@Overridepublic String toString() {return "Student{" +"id='" + id + '\'' +", name='" + name + '\'' +", age=" + age +", birthday=" + birthday +", teacherList=" + teacherList +", teacher=" + teacher +", data=" + data +", dataList=" + dataList +", sex=" + sex +", weight=" + weight +'}';}}
public class Teacher {private String id;private String name;private Integer age;public Teacher() {}public Teacher(String id, String name, Integer age) {this.id = id;this.name = name;this.age = age;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Teacher{" +"id='" + id + '\'' +", name='" + name + '\'' +", age=" + age +'}';}}
public class Main {public static void main(String[] args) {Student stu = new Student();initData(stu);//bean转为json字符串beanToJson(stu);//json字符串转为beanString jsonStr = "{\"age\":25,\"birthday\":1543657701694,\"data\":{\"1\":\"小罗\",\"2\":\"小嘉\",\"3\":\"小敏\"},\"dataList\":[{\"1\":\"小罗\",\"2\":\"小嘉\",\"3\":\"小敏\"},{\"1\":\"小周\",\"2\":\"小蕾\"}],\"id\":\"007\",\"name\":\"小西\",\"sex\":0,\"teacher\":{\"age\":22,\"id\":\"1\",\"name\":\"佳佳\"},\"teacherList\":[{\"age\":22,\"id\":\"1\",\"name\":\"小明\"},{\"age\":22,\"id\":\"2\",\"name\":\"小红\"},{\"age\":22,\"id\":\"3\",\"name\":\"小志\"}],\"weight\":62.5}";jsonToBean(jsonStr);//方式2:json字符串转为beanjsonToBean2(jsonStr);}public static void beanToJson(Student stu) {//解析为json字符串的时候关闭循环引用String jsonStr = JSON.toJSONString(stu, SerializerFeature.DisableCircularReferenceDetect);System.out.println(jsonStr);}public static void jsonToBean(String jsonStr) {/** 两种方式反序列化 **/
//        Student stu = JSON.parseObject(jsonStr, Student.class);Student stu = JSON.parseObject(jsonStr, new TypeReference<Student>(){});System.out.println(stu);}public static void jsonToBean2(String jsonStr) {JSONObject jsonObject = JSON.parseObject(jsonStr);Student stu = JSON.parseObject(jsonObject.toJSONString(), new TypeReference<Student>(){});System.out.println(stu);}public static void initData(Student stu) {stu.setId("007");stu.setName("小西");stu.setAge(25);stu.setBirthday(new Date());stu.setSex(0);stu.setWeight(62.5f);//list集合,set集合一样ArrayList<Teacher> list = new ArrayList<>();list.add(new Teacher("1","小明",22));list.add(new Teacher("2","小红",22));list.add(new Teacher("3","小志",22));stu.setTeacherList(list);//在javaBean中添加javaBeanTeacher teacher = new Teacher("1","佳佳",22);stu.setTeacher(teacher);//添加map集合Map<String, String> map = new HashMap<>();map.put("1", "小罗");map.put("2", "小嘉");map.put("3", "小敏");stu.setData(map);//添加map泛型的listList<Map<String, String>> dataList = new ArrayList<>();Map<String, String> map2 = new HashMap<>();map2.put("1", "小周");map2.put("2", "小蕾");dataList.add(map);dataList.add(map2);stu.setDataList(dataList);}}

FastJson之json字符串与javabean的互相转换相关推荐

  1. fastjson对json字符串JSONObject和JSONArray互相转换操作示例

    package com.tapt.instance; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; ...

  2. fastjson对json字符串JSONObject和JSONArray互相转换操作示例java

    package com.tapt.instance; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; ...

  3. 用FastJson将JSON字符串转Json

    一.导入jar <!--fastjson--><dependency><groupId>com.alibaba</groupId><artifac ...

  4. fastjson的json字符串转List

    fastjson的json字符串转List pom依赖 <dependency><groupId>com.alibaba</groupId><artifact ...

  5. Json对象与Json字符串的转化、JSON字符串与Java对象的转换

    一.Json对象与Json字符串的转化 1.jQuery插件支持的转换方式: $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符 ...

  6. json java typeof_Json对象与Json字符串的转化、JSON字符串与Java对象的转换

    一.Json对象与Json字符串的转化 1.jQuery插件支持的转换方式: $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符 ...

  7. JSON字符串和对象之间的转换

    使用jackSON来实现JSON字符串和对象之间的转换 引入maven <!--jackson--><dependency><groupId>com.fasterx ...

  8. 一文教你json字符串与JavaBean对象如何相互转换

    工作中经常会遇到在复杂的业务场景中,将数据持久化时某个字段存储的是json字符串,取出数据进行操作时,不能直接对json字符串进行操作,能操作的是JavaBean对象. 或者调用其他的服务(Java应 ...

  9. fastjson 返回json字符串,JSON.parse 报错

    这是由于转义字符引起的如 : \ , fastjson 处理后是双反斜杠:\\ ,而 JSON.parse 解析时需要4个反斜杠 ,即 js解析json 反斜杠时,需要 4个 解成 1 个 解决方法: ...

最新文章

  1. EasyUI学习总结(五)——EasyUI组件使用
  2. 多数据源处理-苞米豆-dynamic
  3. 小话设计模式四:策略模式
  4. Linux中文件颜色所代表的属性和颜色
  5. AjaxPro 未定义错误
  6. Linux系统安全之pam后门安装使用详解
  7. ROS路由器脚本文件编辑更新器
  8. 【软工】week3-个人阅读作业-软件案例分析
  9. vscode 折叠/展开所有区域代码快捷键
  10. JAVA编程入门学习
  11. 存储洞察:从技术到市场
  12. html 字体图标转换工具,HTML5 webfont字体图标的使用
  13. VRTK_Artificial Rotator(人工旋转器)脚本属性详解
  14. 全国计算机等级考试证书电子,全国计算机等级考试证书效力
  15. php实现远程下载文件到本地服务器指定目录
  16. GUID MBR新一代分区表 DiskGenius
  17. 百度地图标记点中添加echarts图表
  18. ASP.NET 母版页小实例(点击显示文本内容)
  19. (转)Native Extensions for Silverlight (NESL)?
  20. hibernate映射文件set key one-to-many 解释

热门文章

  1. USB3.0移动硬盘启动Win7的方法(AHCI/AMD USB3.0/Win7)
  2. 哔哩哔哩在港溢价发行:上市材料出错被批评,暗盘已破发
  3. VBA发送email
  4. Centos7安装Torque
  5. lisp 焊缝标注_德国图纸焊接符号
  6. WORKNC 2021.0中文版软件+安装教程
  7. 《2019-刘宏伟-单通道超材料孔径雷达成像算法研究》论文学习
  8. python线上课堂_线上线下相结合的Python编程教学
  9. linux绑定ip mac地址,人文网-Linux系统绑定IP和MAC地址
  10. 中国温室灌溉系统市场趋势报告、技术动态创新及市场预测