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

多嵌套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 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败