英文:
Passing date in json file in KarateFramwork
问题
我有以下函数来获取当前日期:
function() {
var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
var sdf = new SimpleDateFormat('yyyyMMdd');
var date = new java.util.Date();
return sdf.format(date);
}
然后我将日期传递给JSON文件如下:
* def currDate = getDate()
JSON文件:
{
"clientId": "ABC",
"serviceLine": "DSS",
"locationId": "VOL",
"serviceType": "ADC",
"attendanceDate": #(currDate),
"saId": "123",
"attendance": "PRESENT",
"attendanceType": "ATTENDANCE"
}
JSON默认会将日期转换为字符串,例如"20230210"(yyyyMMdd)格式。这个格式是正常的并且是预期的。但是,当我将变量currDate
传递给JSON时,它会将其转换为带有引号的字符串,例如"20230210",但请求应该是没有引号的日期,即"20230210"。否则,会导致无效输入错误。
如何在JSON文件中将字符串转换为日期格式?
英文:
I have following function to get current date:
function() {
var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
var sdf = new SimpleDateFormat('yyyyMMdd');
var date = new java.util.Date();
return sdf.format(date);
}
And I'm passing the date to JSON file as follows:
* def currDate = getDate()
JSON File:
{
"clientId": "ABC",
"serviceLine": "DSS",
"locationId": "VOL",
"serviceType": "ADC",
"attendanceDate": #(currDate),
"saId": "123",
"attendance": "PRESENT",
"attendanceType": "ATTENDANCE"
}
Json by default converts date into string for e.g. "20230210" (yyyyMMdd). This format is fine & as expected. But just I pass the variable currDate to JSON, it converts it into String with "", e.g., "20230210" but request should have date without quotes, i.e. just 20230210. Otherwise it gives error as Invalid Input.
How can I convert string to date format in JSON file?
答案1
得分: 0
在看到您的评论后,看起来您只是想将日期格式的输出用作数字。这很简单,只需执行以下操作:
def currDate = 1 * getDate()
还可以参考:https://github.com/karatelabs/karate#floats-and-integers
英文:
After seeing your comments, looks like you are just trying to use the date format output as a number. That's easy, just do this:
* def currDate = 1 * getDate()
Also refer: https://github.com/karatelabs/karate#floats-and-integers
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论