博客
关于我
JSON、JSONObject、JavaBean三者的相互转换
阅读量:582 次
发布时间:2019-03-11

本文共 1572 字,大约阅读时间需要 5 分钟。

JSON、JSONObject、JavaBean三者的相互转换

平常的开发中,json的转换是需要经常使用到的,特意整理了下。

User user = new User(1L, "haha", 13, "erfa",new Date(),new Date(),1,3);@Testvoid testFastJson() {    //将对象转换成json字符串    String userToJsonString = JSON.toJSONString(user);    System.out.println(userToJsonString);    //将json字符串转换成对象    User jsonStringToUser = JSON.parseObject(userToJsonString,User.class);    System.out.println(jsonStringToUser);}/** * JavaBean转JSONObject */@Testvoid JavaBeanToJSONObject(){    JSONObject jsonObject = (JSONObject)JSONObject.toJSON(user);    System.out.println(jsonObject.toString());}/** * JavaBean转JSON */@Testvoid JavaBeanToJSON(){    String json = JSON.toJSONString(user);    System.out.println(json);}/** * JSONObject转JavaBean */@Testvoid JSONObjectToJavaBean() {    JSONObject jsonObject = (JSONObject)JSONObject.toJSON(user);    User user = jsonObject.toJavaObject(User.class);    System.out.println(user.toString());}/** * JSONObject转Json */@Testvoid JSONObjectToJson() {    JSONObject jsonObject = (JSONObject)JSONObject.toJSON(user);    String json = JSONObject.toJSONString(jsonObject);    System.out.println(json);}/** *Json转JavaBean */@Testvoid JsonToJavaBean() {    String json = JSON.toJSONString(user);    User user = JSON.parseObject(json, User.class);    System.out.println(user.toString());}/** * Json转JSONObject */@Testvoid JsonToJSONObject() {    String json = JSON.toJSONString(user);    JSONObject jsonObject = JSONObject.parseObject(json);    System.out.println(jsonObject);    String name = jsonObject.getString("name");    System.out.println(name);}

转载地址:http://owgvz.baihongyu.com/

你可能感兴趣的文章
Node-RED中Button按钮组件和TextInput文字输入组件的使用
查看>>
vue3+Ts 项目打包时报错 ‘reactive‘is declared but its value is never read.及解决方法
查看>>
Node-RED中Slider滑杆和Numeric数值输入组件的使用
查看>>
Node-RED中Switch开关和Dropdown选择组件的使用
查看>>
Node-RED中使用exec节点实现调用外部exe程序
查看>>
Node-RED中使用function函式节点实现数值计算(相加计算)
查看>>
Node-RED中使用html节点爬取HTML网页资料之爬取Node-RED的最新版本
查看>>
Node-RED中使用JSON数据建立web网站
查看>>
Node-RED中使用json节点解析JSON数据
查看>>