多嵌套json字符串,根据key改value

多嵌套json字符串,根据key改value

变量
String accessToken = Bearer aaaa


```java
{
    "config": {
        "name": "testcase description"
    },
    "teststeps": [
        {
            "name": "",
            "request": {
                "method": "POST",
                "url": "xxxx",
                "headers": {
                    "Accept": "*/*",
                    "Accept-Encoding": "gzip, deflate, br",
                    "Authorization": "Bearer bbbb",
                    "Connection": "keep-alive",
                    "Content-Length": "156",
                    "Content-Type": "application/json",
                    "Host": "xxxx",
                    "Postman-Token": "xxx",
                    "User-Agent": "PostmanRuntime/7.28.4"
                },
                "cookies": {
                    "JSESSIONID": "xxxx"
                },
                "body": {
                    "chunkCount": xx,
                    "fileMd5": "x",
                    "fileName": "xxx",
                    "fileSize": xx,
                    "folderName": "xxx",
                    "partNumber": x,
                    "uploadStatus": "",
                    "userId": "xxx"
                }
            },
            "validate": [
                {
                    "check": "status_code",
                    "assert": "equals",
                    "expect": 200,
                    "msg": "assert response status code"
                }
            ]
        },
        {
            "name": "",
            "request": {
                "method": "POST",
                "url": "xxxx",
                "headers": {
                    "Accept": "*/*",
                    "Accept-Encoding": "gzip, deflate, br",
                    "Authorization": "Bearer cccc",
                    "Connection": "keep-alive",
                    "Content-Length": "88",
                    "Content-Type": "application/json",
                    "Host": "xxx",
                    "Postman-Token": "xxx",
                    "User-Agent": "PostmanRuntime/7.28.4"
                },
                "cookies": {
                    "JSESSIONID": "xxx"
                },
                "body": {
                    "fileMd5": "xxx",
                    "fileName": "xx",
                    "uploadStatus": x
                }
            },
            "validate": [
                {
                    "check": "status_code",
                    "assert": "equals",
                    "expect": 200,
                    "msg": "assert response status code"
                }
            ]
        }
    ]
}



###### 变量accessToken = Bearer aaaa 替换 代码黑体部分 Bearer bbbb 和 Bearer cccc 的效果如下:


{
    "config": {
        "name": "testcase description"
    },
    "teststeps": [
        {
            "name": "",
            "request": {
                "method": "POST",
                "url": "xxxx",
                "headers": {
                    "Accept": "*/*",
                    "Accept-Encoding": "gzip, deflate, br",
                    "Authorization": "Bearer aaaa",
                    "Connection": "keep-alive",
                    "Content-Length": "156",
                    "Content-Type": "application/json",
                    "Host": "xxxx",
                    "Postman-Token": "xxx",
                    "User-Agent": "PostmanRuntime/7.28.4"
                },
                "cookies": {
                    "JSESSIONID": "xxxx"
                },
                "body": {
                    "chunkCount": xx,
                    "fileMd5": "x",
                    "fileName": "xxx",
                    "fileSize": xx,
                    "folderName": "xxx",
                    "partNumber": x,
                    "uploadStatus": "",
                    "userId": "xxx"
                }
            },
            "validate": [
                {
                    "check": "status_code",
                    "assert": "equals",
                    "expect": 200,
                    "msg": "assert response status code"
                }
            ]
        },
        {
            "name": "",
            "request": {
                "method": "POST",
                "url": "xxxx",
                "headers": {
                    "Accept": "*/*",
                    "Accept-Encoding": "gzip, deflate, br",
                    "Authorization": "Bearer aaaa",
                    "Connection": "keep-alive",
                    "Content-Length": "88",
                    "Content-Type": "application/json",
                    "Host": "xxx",
                    "Postman-Token": "xxx",
                    "User-Agent": "PostmanRuntime/7.28.4"
                },
                "cookies": {
                    "JSESSIONID": "xxx"
                },
                "body": {
                    "fileMd5": "xxx",
                    "fileName": "xx",
                    "uploadStatus": x
                }
            },
            "validate": [
                {
                    "check": "status_code",
                    "assert": "equals",
                    "expect": 200,
                    "msg": "assert response status code"
                }
            ]
        }
    ]
}

```

自己记录一下吧,不知哪位大神有简单的方式。



```java
//文件读取字符串
            jsonStr = sb.toString();
            //转jsonObject
            org.json.JSONObject jsonObject1 = new org.json.JSONObject(jsonStr);
            //过滤teststeps得到String
            String str1 = jsonObject1.getString("teststeps");
            org.json.JSONArray jsonArray1 = new org.json.JSONArray(str1);
            for (int i = 0; i < jsonArray1.length(); i++) {
                org.json.JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
                String str2 = jsonObject2.getString("request");
                org.json.JSONObject jsonObject3 = new org.json.JSONObject(str2);
                String str3 = jsonObject3.getString("headers");
                org.json.JSONObject jsonObject4 = new org.json.JSONObject(str3);
                jsonObject4.put("Authorization", accessToken);
                jsonObject3.put("headers", jsonObject4.toString());
                jsonObject2.put("request", jsonObject3);
                jsonArray1.put(i, jsonObject2);
            }

```

循环 根据 唯一标识 修改 key