空手道:如何将对象/地图中的字符串从javascript转换为Java? [英] Karate: how to convert object/map in string from javascript to Java?

查看:44
本文介绍了空手道:如何将对象/地图中的字符串从javascript转换为Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为production_2.json的文件json

I Have a file json named production_2.json

[
  {
     "v":{
        "id":"rep_01564526",
        "name":"tuttoverde.pgmx",
        "type":"PRODUCTION_STARTED",
        "ute":"CDL",
        "ver":"1.0",
        "status":"EXE"
     },
     "ts":"2020-11-19T08:00:00Z"
  },
  {
     "v":{
        "id":"rep_01564526",
        "name":"tuttoverde.pgmx",
        "type":"PRODUCTION_ENDED",
        "ute":"CDL",
        "ver":"1.0",
        "status":"EXE"
        
     },
     "ts":"2020-11-19T17:00:00Z"
  }
]

并具有以下空手道代码,

And have the folling Karate code, that:

  1. 读取文件production_2.json
  2. 并为数组的每个元素发送一个主题

I  * def sendtopics = 
     """
         function(i){
           
            var topic = "data." + machineID + ".Production";
            var payload = productions[i];
            karate.log('topic: ', topic )
            karate.log('payload: ', payload )
            return  mqtt.sendMessage(payload, topic);
            
         }
     """

    * def productions =  read('this:productions_json/production_2.json')
    * def totalProductionEvents = productions.length
    * def isTopicWasSent =  karate.repeat(totalProductionEvents, sendtopics)
    * match each isTopicWasSent == true

功能

mqtt.sendMessage(payload, topic);

是Java中的函数,具有以下特征

is a function in java, that have the following segnature

public Boolean sendMessage(String payload, String topic) {

        System.out.println("Publishing message: ");
        System.out.println("payload " + payload);
        System.out.println("topic " + topic);
        return true;
      }

问题是,javascript函数内的有效负载" 的值正确无误,并且在正确打印了.有效载荷的值格式错误.

the problem is that the value of the "payload" inside the javascript function is correct and is printed correctly, while inside the "sendMessage" function the value of the payload is formatted incorrectly.

例如,这是它在karate.log('payload:',payload)中打印的内容

For example here is what it prints inside karate.log('payload: ', payload )

payload:  {
  "v": {
    "id": "rep_01564526",
    "name": "tuttoverde.pgmx",
    "type": "PRODUCTION_STARTED",
    "ute": "CDL",
    "ver": "1.0",
    "status": "EXE"
  },
  "ts": "2021-01-08T08:00:00Z"
}

这里,在函数"sendMessage"上打印的是类的

And Here instead what is printed on the function "sendMessage" of the java class

payload {v={id=rep_01564526, name=tuttoverde.pgmx, type=PRODUCTION_STARTED, ute=CDL, ver=1.0, status=EXE,  ts=2021-01-08T08:00:00Z}

我不明白为什么有效载荷的格式不正确(=而不是:),并且它不是字符串.我也尝试过使用以下解决方案,但它对我不起作用

I don't understand why the payload is formatted incorrectly (= instead of : ) and is it not a string. I also tried using the following solution and it doesn't work for me

 * def sendtopics = 
     """
         function(i){
           
            var topic = "data." + machineID + ".Production";
            var payload = productions[i];
            var payload2 = JSON.stringify(payload);
            return  mqtt.sendMessage(payload2, topic);
            
         }
     """

如何将javascript中的对象转换为字符串,以便可以将其传递给java?

How do I convert an object inside javascript to a string so I can pass it to java?

推荐答案

您正在用空手道做一些非常高级的事情.我强烈建议您开始查看新版本(即将发布),并在此处找到详细信息:

You are doing some really advanced stuff in Karate. I strongly suggest you start looking at the new version (close to release) and you can find details here: https://github.com/intuit/karate/wiki/1.0-upgrade-guide

原因是因为异步和Java互操作进行了一些重大更改-并且可以在JS中的 karate 对象上调用一些新的API,以进行格式转换:

The reason is because the async and Java interop has some breaking changes - and there are some new API-s you can call on the karate object in JS to do format conversions:

var temp = karate.fromString(payload);

并且 karate.log()应该会更好,并且不会给您带来您所抱怨的奇怪格式.如果使用当前版本,则可以尝试 karate.toJson()来实现预期的转换.

And karate.log() should work better and not give you that odd formatting you are complaining about. With the current version you can try karate.toJson() if that gives you the conversion you expect.

鉴于您的高级用法,我建议您开始使用新版本并就可能仍需要的任何内容提供反馈.

Given your advanced usage, I recommend you start using the new version and provide feedback on anything that may be still needed.

这篇关于空手道:如何将对象/地图中的字符串从javascript转换为Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆