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);
  • 相关阅读:
    Hibernate与数据库的触发器协同工作
    Hibernate的调用数据库的存储过程
    hibernate中持久化对象的状态
    Hibernate-sessio缓存的操作
    Hibernate中的一些关键字理解
    配置Hibernate的流程
    Struts2自定义拦截器
    Struts2中解决表单重复提交
    Struts文件下载(静态)
    Struts2的简单的文件上传
  • 原文地址:https://www.cnblogs.com/xuzhongyin/p/12971486.html
Copyright © 2011-2022 走看看