转载

掘金者说-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
正文到此结束
Loading...