咏六郎 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 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?
  • ¥15 html5 qrcode 扫描器
  • ¥15 爬取网页信息并保存需要完整代码