You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决BeanShell更新JSON文件时的FileUtils语法错误?

解决BeanShell更新JSON文件时的语法错误问题

错误原因分析

你遇到的错误是语法解析错误,根源在脚本第17行代码末尾缺少分号,导致BeanShell解析器无法正确识别后续的FileUtils代码,从而抛出异常。

修复步骤

1. 补全缺失的分号

找到这行代码:

jsonObject.put("caseConfigId", extractedValue2)

在末尾添加分号,修正为:

jsonObject.put("caseConfigId", extractedValue2);

2. 修复潜在类型转换问题

jsonObject.get("caseType")返回的是Object类型,直接调用put可能会触发类型转换错误,需要强转为JSONObject:

((JSONObject) jsonObject.get("caseType")).put("id", extractedValue1);

3. 补充必要的导入(可选但推荐)

脚本中使用了File类,建议显式导入,避免潜在的类找不到问题:

import java.io.File;

修复后的完整脚本

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.apache.commons.io.FileUtils;
import java.io.File;

try {
    String filePath = "blob";
    File file = new File(filePath);
    String jsonContent = FileUtils.readFileToString(file, "UTF-8");

    JSONParser parser = new JSONParser();
    JSONObject jsonObject = (JSONObject) parser.parse(jsonContent);

    String extractedValue1 = vars.get("CASE_TYPE_ID");
    ((JSONObject) jsonObject.get("caseType")).put("id", extractedValue1);
    
    String extractedValue2 = vars.get("CASE_CONFIG_ID");
    jsonObject.put("caseConfigId", extractedValue2);

    FileUtils.writeStringToFile(file, jsonObject.toJSONString(), "UTF-8");
} catch (Exception e) {
    log.error("Error updating JSON file: " + e.getMessage());
}

额外检查

确保commons-io-x.x.x.jar和json-simple-x.x.x.jar已放置在JMeter的lib目录下,重启JMeter后再运行脚本,避免出现类找不到的异常。

内容的提问来源于stack exchange,提问作者Agathiyan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 21:34:57