As a quick note, calling a third-party interface via RPC returns json in string format

{"error":"0"."error_description":"success"."body": {"access_token":"369a3819da3c457e9ff9a5e8a660a2ec"."refresh_token":"ebfd3fcf543345eebd3ee53e0ef34c67"."machine_code":""."expires_in":2592000."scope":"all"}}
Copy the code

Use the tool Coolformat.exe to output formatting as follows

{
   "body" : {
      "access_token" : "369a3819da3c457e9ff9a5e8a660a2ec"."expires_in" : 2592000,
      "machine_code" : ""."refresh_token" : "ebfd3fcf543345eebd3ee53e0ef34c67"."scope" : "all"
   },
   "error" : "0"."error_description" : "success"
}
Copy the code

Resolution 1

Introduce dependency jSON-lib

<dependency>
	<groupId>net.sf.json-lib</groupId>
	<artifactId>json-lib</artifactId>
	<version>2.2.2</version>
	<classifier>jdk15</classifier>
</dependency>
Copy the code

Process through Java

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

public class JsonStrAnalysis {

    @Test
    public void testJsonString(a) {
        String objectStr = "{\"error\":\"0\",\"error_description\":\"success\",\"body\":{\"access_token\":\"369a3819da3c457e9ff9a5e8a660a2ec\",\"re fresh_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")); }}Copy the code

Print as follows:

{"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

Copy the code

Parsing 2

Use IDEA plugin GsonFormat plugin, shortcut Alt+Insert, generate an entity class

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; / / the Get and Set complement public static class BodyBean {/ * * * access_token: 369 a3819da3c457e9ff9a5e8a660a2ec * 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 StringtoString() {
            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 + '\' ' +
                '} '; }}Copy the code

Then, parse as follows:

Processing through Java Javaimport net.sf.json.JSONObject;
import org.testng.annotations.Test;

public class JsonStrAnalysis {

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

        // use JSONObjectGsonBody gsonBody = (GsonBody) JSONObject.toBean(jsonObject, GsonBody.class); System.out.println(gsonBody.toString()); System.out.println(gsonBody.getBody().toString()); }}Copy the code

Results print:


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

Copy the code