简体   繁体   English

空手道javaScript数组格式

[英]karate javaScript array format

Here is my javascript which prepares json which I will use for matching an api response 这是我的javascript,它准备用于匹配api响应的json

var squArray = []
for (j = loopincri; j < (loopincri + skuCount); j++) {
    var skuJson = {
        "skuCode": skuCodes[j],
        "quantity": quantity
    }
    squArray.push(skuJson)
}

when I am printing this is coming like: 当我打印时,这是这样的:

{
  "0": {
    "sku_code": "50",
    "quantityNO": 2,
    "min": 550,
    "max": 13000,
    "dtePrice": {
      "0": {
        "date": "2019-04-11",
        "listPrice": 6600,
        "salePrice": 3870
      },
      "1": {
        "date": "2019-04-12",
        "listPrice": 6600,
        "salePrice": 3870
      }
    }
  }
}

But I want the result to be like 但我希望结果像

[
  {
    "sku_code": "50",
    "quantityNO": 2,
    "min": 550,
    "max": 13000,
    "dtePrice": [
      {
        "date": "2019-04-11",
        "listPrice": 6600,
        "salePrice": 3870
      },
      {
        "date": "2019-04-12",
        "listPrice": 6600,
        "salePrice": 3870
      }
    ]
  }
]

Can you please help me out with this? 你能帮我这个忙吗? As the response of the api is in the format I want 由于API的响应是我想要的格式

Yes there is an issue with arrays if you do too much in JS: https://stackoverflow.com/a/54256766/143475 - so if you do karate.read() to create the chunks of JSON, it should start working. 是的,如果您在JS中执行过多操作,则数组存在问题: https : karate.read()因此,如果您执行karate.read()创建JSON块,它应该开始工作。

My recommendation is avoid using JS as far as possible, try to use Karate natives for looping and transformation etc. For example: 我的建议是尽可能避免使用JS,请尝试使用Karate本机进行循环和转换等。例如:

* def skuCodes = ['a', 'b', 'c', 'd', 'e']
* def nums = []
* eval for(var i = 0; i < 5; i++) nums.add(~~i)
* def fun = function(x){ return { skuCode: skuCodes[x], array: read('array.json') } }
* def json = karate.map(nums, fun)

Yes, using read() is a slight inconvenience, but note that you can use embedded expressions within a JSON file. 是的,使用read()会带来一些不便,但是请注意,您可以在JSON文件中使用嵌入式表达式。 This will be fixed in a future version of Karate. 这将在空手道的未来版本中修复。

This could be workaround for overcoming that jdk bug discussed in https://stackoverflow.com/a/54256766/8615449 这可能是克服https://stackoverflow.com/a/54256766/8615449中讨论的jdk错误的解决方法

Instead of returning your object directly, try, 与其直接返回您的对象,不如尝试,

return Java.type("com.intuit.karate.JsonUtils").toJson(squArray)

I know this return JSON in a string format, you can cast it in your feature by 我知道此返回字符串格式的JSON,您可以通过以下方式将其转换为功能

* json myJson = JsFunction()

here JsFunction() is the function that you created above which returns that json value. 这里的JsFunction()是您在上面创建的函数,该函数返回该json值。

Don't know about karate JS. 不知道空手道JS。 But if karate JS is converting your array to object then you can reconvert to array. 但是,如果空手道JS将数组转换为对象,则可以将其转换为数组。 As you can see that the resulting object is arraylike which can easily be converted to array using "Array.from(queryObject)" method. 如您所见,生成的对象类似于array,可以使用“ Array.from(queryObject)”方法轻松将其转换为array。 Modification :- 修改 :-

var squArray = []
for(j=loopincri ; j<(loopincri+skuCount); j++){
        var skuJson = {"skuCode":skuCodes[j],
                       "quantity":quantity};
        squArray.push(skuJson);
    }
let squArray = Array.from(squArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM