JSON常用方法

转自:https://blog.csdn.net/wts563540/article/details/108418454

1、JSON.parseObject和JSON.toJSONString

    JSON.parseObject,是将Json字符串转化为相应的对象;JSON.toJSONString则是将对象转化为Json字符串。在前后台的传输过程中,JSON字符串是相当常用的,这里就不多介绍其功能了,直接举一下应用的小例子,帮助理解这两个方法的用法。

首先用maven引入fastjson

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
     
        <groupId>com.wujiang.test</groupId>
        <artifactId>test</artifactId>
        <version>1.0-SNAPSHOT</version>
     
        <properties>
            <fastjson_version>1.2.28</fastjson_version>
        </properties>
     
        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson_version}</version>
            </dependency>
        </dependencies>
    </project>

定义一个model类,员工,有四个属性,如下所示:

 @Data
    public class Staff {
        private String name;
        private Integer age;
        private String sex;
        private Date birthday;
   
        @Override
        public String toString() {
            return "Staff{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", sex='" + sex + '\'' +
                    ", birthday=" + birthday +
                    '}';
        }
    }

好的,下一步,测试一下JSON.parseObject 和 JSON.toJSONString方法。这里故意在Json字符串中多了一个telephone,少了一个Staff中的birthday,看看输出的对象会有什么变化

    public class jsonTest {
        public static void main(String[] args) {
            /**
             * json字符串转化为对象
             */
            String jsonString = "{name:'Antony',age:'12',sex:'male',telephone:'88888'}";
            Staff staff = JSON.parseObject(jsonString, Staff.class);
            System.out.println(staff.toString());
     
            /**
             * 对象转化为json字符串
             */
            String jsonStr = JSON.toJSONString(staff);
            System.out.println(jsonStr);
        }
    }

输出结果
    Staff{name='Antony', age=12, sex='male', birthday=null}
    {"age":12,"name":"Antony","sex":"male"}
    //如果age是String类型,那么输出结果变为
    //{"age":"12","name":"Antony","sex":"male"}

在JSON.parseObject 的时候,会去填充名称相同的属性。对于Json字符串中没有,而model类有的属性,会为null;对于model类没有,而Json字符串有的属性,不做任何处理。

至于 JSON.toJSONString 就不需要多说了,看一下就知道

至于应用场景,比方说,用户登录微信公众号的时候,调用微信官方的restful接口,得到该用户的所有信息的一个Json字符串,然后写一个类(将自己需要的信息封装成一个类)。例如下面的伪代码

    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);

2、JSON.parseArray

这个方法的作用就是将json格式的数据转换成数组格式。

假设有Person这个类,有json类型数据jsonStr = [{"name":"张三","age":"1"},{"name":"李四","age":"4"}],那么List lists = json.parseArray(jsonStr, Person.class);lists就可以接收jsonStr了
三者区别:
1、toJSONString
    String str = JSON.toJSONString(Entity);

2、parseObject
    Entity toObj = JSON.parseObject(str,  Entity.class);

3、parseArray
    String arrJson = JSON.toJSONString(entityList);
    List<Entity> arrList = JSON.parseArray(arrJson, Entity.class);

 

posted @   sensen~||^_^|||&  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示