Karate框架CSV转JSON时整数转换失败问题求助
Karate框架CSV转JSON整数转换异常解决方案
问题背景
使用Karate框架从CSV文件解析数据生成JSON时,遇到两个核心问题:
- 字符串转整数后生成的JSON数值始终为浮点数格式(如
10.0),无法得到纯整数10 - 出现反序列化错误:
JSON parse error: Cannot deserialize value of type java.lang.Integer from String "(1 * total_amount)": not a valid java.lang.Integer value,CSV中total_amount实际值为1000
尝试过两种转换方式均未解决浮点数问题:
- 定义
def foo = '10',通过string json = { bar: '#(1 * foo)' }生成{"bar":10.0} - 使用
string json = { bar: '#(parseInt(foo))' },结果仍为{"bar":10.0}
环境配置(POM)
<groupId>org.karateauto</groupId> <artifactId>Karate-Lead_Demo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>11</java.version> <maven.compiler.version>3.11.0</maven.compiler.version> <maven.surefire.version>3.0.0</maven.surefire.version> <karate.version>1.4.1</karate.version> </properties> <dependencies> <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-junit5</artifactId> <version>${karate.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-core</artifactId> <version>${karate.version}</version> <classifier>all</classifier> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> </dependencies> <build> <testResources> <testResource> <directory>src/test/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </testResource> </testResources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.version}</version> <configuration> <encoding>UTF-8</encoding> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${maven.surefire.version}</version> <configuration> <argLine>-Dfile.encoding=UTF-8</argLine> </configuration> </plugin> </plugins> </build>
原JSON模板
{ "action": "#(action)", "currency": "#(currency)", "cptrid": "#(cptrid)", "flexi1": "#(flexi1)", "flexi2": "#(flexi2)", "flexi3": "#(flexi3)", "flexi4": "#(flexi4)", "sys_id": "#(sys_id)", "lead_id": "#(lead_id)", "record_id": "#(record_id)", "mode_of_payment": "#(mode_of_payment)", "total_amount": "#(1 * total_amount)", "instrument_ref": "#(instrument_ref)", "instrument_date": "#(instrument_date)", "bank_name": "#(bank_name)", "branch_detls": "#(branch_detls)", "receipt_no": "#(receipt_no)" }
解决方案
1. 生成纯整数格式JSON
Karate中直接通过字符串插值生成JSON时,数值默认会被处理为浮点数。需先将字符串转为整数类型,再填充模板:
# 方式一:先转换类型再生成JSON def foo = '10' def bar = parseInt(foo) string json = { bar: '#(bar)' } # 方式二:用karate.toJson强制输出整数 def data = { bar: parseInt(foo) } string json = karate.toJson(data)
2. 修复CSV字段反序列化错误
错误原因是模板中的表达式(1 * total_amount)未被正确求值,被当作字符串传递。需先处理CSV数据类型,再填充模板:
方案A:加载CSV时批量转换字段类型
# 读取CSV并转换total_amount为整数 def csvData = read('你的CSV文件路径.csv') def processedData = karate.map(csvData, (item) => { item.total_amount = parseInt(item.total_amount) return item }) # 使用处理后的数据生成请求体 def payload = read('template.json')
方案B:在JSON模板中直接调用类型转换方法
修改模板中total_amount的表达式:
{ // 其他字段保持不变 "total_amount": "#(karate.number(total_amount).intValue())" }
3. 关键说明
Karate 1.4.x版本读取CSV时,所有字段默认都是字符串类型,必须显式转换为数值类型才能避免序列化异常。尽量避免在模板中直接写复杂表达式,建议先在步骤中完成数据类型处理,再填充模板。
内容的提问来源于stack exchange,提问作者bulbasorus
相关产品推荐
相关产品推荐

