zoukankan      html  css  js  c++  java
  • JSON方法

    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);
  • 相关阅读:
    稀疏矩阵解题数学库 -- UMFPACK
    国外程序猿整理的C++大全
    SQL实用语句大全
    this 三句话
    ELK 7.4.2 单机安装配置
    简单搭建DNS服务器——bind
    关于博客皮肤
    Golang 实现 array_push
    Golang 发送POST请求,加header头,带参数
    Golang 签名
  • 原文地址:https://www.cnblogs.com/xuzhongyin/p/12971486.html
Copyright © 2011-2022 走看看