首页 > Web开发 > 详细

JSON方法

时间:2020-05-27 12:23:28      阅读:37      评论:0      收藏:0      [点我收藏+]
  1. 对象或List进行JSON转化: 

String jsonStr = JSON.toJSONString(tableInfoVO.getFieldInfo());
 

         2. JSON字符串转化为对象:

https://www.cnblogs.com/dianzan/p/11332672.html

如下两个例子:

JSONObject jsonobject = JSON.parseObject(str);
或者:
JSONObject jsonobject = JSONObject.parseObject(str);

String s = httpRequest.sendGet("https://api.weixin.qq.com/sns/oauth2/access_token","appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code");
UserAuthorizationReturn userAuthorizationReturn = JSON.parseObject(s, UserAuthorizationReturn.class);

具体例子:
public class Staff {
   private String name;
   private int age;
   private String sex;
   private Date birthday;
}
public static void main(String args[]) {
   //json字符串转化为对象
   String jsonString = "{name:‘yxs‘,age:23,sex:‘man‘,telephone:‘12346‘}";
   Staff staff = JSON.parseObject(jsonString, Staff.class);
   System.out.println(staff.toString());
   //对象转化为json字符串

   String string = JSON.toJSONString(staff);
   System.out.println(string);

}
//输出结果
Staff{name=‘yxs‘, age=23, sex=‘man‘, birthday=null}
{"age":23,"name":"yxs","sex":"man"}
总结:在JSON。parseObject的时候,回去填充名字相同的属性,对于Json字符串中没有,而model类中有的属性,会为null,对于model类中没有,而json字符串中有的属性,不做任何处理

        3. JSON字符串转化为List:

String jsonStr = JSON.toJSONString(tableInfoVO.getFieldInfo());
List<FiledInfoVO> filedInfoVOList = JSON.parseArray(jsonStr,FiledInfoVO.class);

JSON方法

原文:https://www.cnblogs.com/xuzhongyin/p/12971486.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!