在Karate框架中传递JSON文件中的日期。

huangapple go评论60阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月10日 14:26:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定