掘金者说-java解析json字符串

做个小笔记,通过RPC调用第三方接口返回一个字符串格式的json

{"error":"0","error_description":"success","body":{"access_token":"369a3819da3c457e9ff9a5e8a660a2ec","refresh_token":"ebfd3fcf543345eebd3ee53e0ef34c67","machine_code":"","expires_in":2592000,"scope":"all"}}
复制代码

使用工具 CoolFormat.exe 输出格式化,如下

{
   "body" : {
      "access_token" : "369a3819da3c457e9ff9a5e8a660a2ec",
      "expires_in" : 2592000,
      "machine_code" : "",
      "refresh_token" : "ebfd3fcf543345eebd3ee53e0ef34c67",
      "scope" : "all"
   },
   "error" : "0",
   "error_description" : "success"
}
复制代码

解析1

引入依赖json-lib

<dependency>
	<groupId>net.sf.json-lib</groupId>
	<artifactId>json-lib</artifactId>
	<version>2.2.2</version>
	<classifier>jdk15</classifier>
</dependency>
复制代码

通过java进行处理

import net.sf.json.JSONObject;
import org.testng.annotations.Test;

public class JsonStrAnalysis {

    @Test
    public void testJsonString() {
        String objectStr = "{/"error/":/"0/",/"error_description/":/"success/",/"body/":{/"access_token/":/"369a3819da3c457e9ff9a5e8a660a2ec/",/"refresh_token/":/"ebfd3fcf543345eebd3ee53e0ef34c67/",/"machine_code/":/"/",/"expires_in/":2592000,/"scope/":/"all/"}}";
        JSONObject jsonObject = JSONObject.fromObject(objectStr);
        System.out.println(jsonObject);
        System.out.println(jsonObject.get("error"));
        System.out.println(jsonObject.get("error_description"));
    }
}
复制代码

打印,如下:

{"error":"0","error_description":"success","body":{"access_token":"369a3819da3c457e9ff9a5e8a660c2ec","refresh_token":"ebfd3fcf543345eebd3ee53e0ef34cd7","machine_code":"","expires_in":2592000,"scope":"all"}}
0
success


===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

复制代码

解析2

使用idea插件 GsonFormat 插件,快捷 Alt+Insert ,生成一个实体类

掘金者说-java解析json字符串
package com.pig4cloud.gson.pojo;

/**
 * @author Lucky
 * @date 2020-06-28
 * @Description //TODO
 */
public class GsonBody {

    /**
     * body : {"access_token":"369a3812da3c457e9ff9a5e8a660c2ec","expires_in":2592000,"machine_code":"","refresh_token":"ebfd3fcf543345eebd3ee53e65f34cd7","scope":"all"}
     * error : 0
     * error_description : success
     */

    private BodyBean body;
    private String error;
    private String error_description;

    // Get and Set 补充

    public static class BodyBean {
        /**
         * access_token : 369a3819da3c457e9ff9a5e8a660a2ec
         * expires_in : 2592000
         * machine_code :
         * refresh_token : ebfd3fcf543445eebd3ee53e0ef34c67
         * scope : all
         */

        private String access_token;
        private int expires_in;
        private String machine_code;
        private String refresh_token;
        private String scope;

        // Get and Set 补充
        
        @Override
        public String toString() {
            return "BodyBean{" +
                    "access_token='" + access_token + '/'' +
                    ", expires_in=" + expires_in +
                    ", machine_code='" + machine_code + '/'' +
                    ", refresh_token='" + refresh_token + '/'' +
                    ", scope='" + scope + '/'' +
                    '}';
        }
    }

    @Override
    public String toString() {
        return "GsonBody{" +
                "body=" + body +
                ", error='" + error + '/'' +
                ", error_description='" + error_description + '/'' +
                '}';
    }
}
复制代码

然后,解析如下:

通过java进行处理
```java
import net.sf.json.JSONObject;
import org.testng.annotations.Test;

public class JsonStrAnalysis {

    @Test
    public void testJson2Obj() {
        String objectStr = "{/"error/":/"0/",/"error_description/":/"success/",/"body/":{/"access_token/":/"369a3819da3c457e9ff9a5e8a660c2ec/",/"refresh_token/":/"ebfd3fcf543345eebd3ee53e0ef34cd7/",/"machine_code/":/"/",/"expires_in/":2592000,/"scope/":/"all/"}}";
        JSONObject jsonObject = JSONObject.fromObject(objectStr);

        //1、使用JSONObject
        GsonBody gsonBody = (GsonBody) JSONObject.toBean(jsonObject, GsonBody.class);
        System.out.println(gsonBody.toString());
        System.out.println(gsonBody.getBody().toString());
    }
}
复制代码

结果打印:

GsonBody{body=BodyBean{access_token='369a3819da3c457e9ff9a5e8a660c2ec', expires_in=2592000, machine_code='', refresh_token='ebfd3fcf543345eebd3ee53e0ef34cd7', scope='all'}, error='0', error_description='success'}
BodyBean{access_token='369a3819da3c457e9ff9a5e8a660c2ec', expires_in=2592000, machine_code='', refresh_token='ebfd3fcf543345eebd3ee53e0ef34cd7', scope='all'}


===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

复制代码

原文 

https://juejin.im/post/5ef83730e51d4534c36d7b30

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » 掘金者说-java解析json字符串

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址