咏六郎 2022-01-17 11:07 采纳率: 100%
浏览 111
已结题

多嵌套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"
                }
            ]
        }
    ]
}

```

  • 写回答

2条回答 默认 最新

  • 咏六郎 2022-01-17 16:00
    关注

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

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

    ```

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月25日
  • 已采纳回答 1月17日
  • 修改了问题 1月17日
  • 创建了问题 1月17日

悬赏问题

  • ¥15 C++ 如何判断设置快捷键来实现隐藏/显示窗口
  • ¥15 关于#材料工程#的问题:有没有具有电子阻挡层和空穴阻挡层的电池仿真silvaco代码例子或者其他器件具有阻挡层例子的silvaco代码(最好还有相关文献)
  • ¥60 基于MATLAB的TAOD算法
  • ¥15 Groimp使用疑问
  • ¥15 MDK–ARM里一直找不到调试器
  • ¥15 oracle中sql查询问题
  • ¥15 vue使用gojs3.0版本,在nodeDataArray中的iconSrc使用gif本地路径,展示出来后动画是静态的,不是动态的
  • ¥100 代写个MATLAB代码,有偿
  • ¥15 ansys electronics 2021 R1安装报错,错误代码2,如图
  • ¥15 Dev-c++打字不出现中文,但出现日文