arrays - 如何在空手道中为 json 数组设置动态值
问题描述
我有一个 json 文件,它需要每个 post 请求的 uniq 值。uniqId = '55555'。对于所有 uniq id,我需要传递相同的 uniqId。到目前为止,我只能为 endtoEndid 设置。无法设置 invoiceNum 和 txnValue。请帮忙。到目前为止,我已经完成了以下工作:
eval req = {"endtoEndid":"", "prgrmId":"2344", "invoices": [ { "invoiceNum":""}], "currency":5.99, "txnRefs": [ { "txnId": “名称”,“txnValue”:“”},{“txnId”:“状态”,“txnValue”:“伊利诺伊州”}]}
eval req['endtoEndId'] = 'endtoEnd' + uniqId。(成功)
- eval req.invoices['invoiceNum'] = 'invoice' + uniqId(不成功)
- eval req['txnRefs.txnValue]'] = 'txn' + uniqId (不成功) 并请求 req
然后我尝试拆分:
- def reqInvoices = req.invoices
def reqTxnRefs = req.txnRefs[0]
eval reqInvoices['invoiceNum'] = 'invoice' + uniqId(不成功)
- eval reqTxnRefs['txnValue'] = 'txn' + uniqId (不成功) 并请求 req
我需要以下结果:
{"endtoEndid":"endToEndIduniqId", "prgrmId":"2344", "invoices": [ { "invoiceNum":"invoiceuniqId"}], "currency":5.99, "txnRefs": [ { "txnId":"名称”,“txnValue”:“txnuniqId”},{“txnId”:“状态”,“txnValue”:“伊利诺伊州”}]}
谢谢
解决方案
请尝试最新版本或 0.9.6.RC3。
现在设置 JSON 要容易得多,您不需要任何eval
或set
.
例子:
* def foo = {}
* foo.bar = 'baz'
* def random = function(){ return java.lang.System.currentTimeMillis() + '' }
* foo.ban = random()
* print foo
这使:
{
"bar": "baz",
"ban": "1591756622099"
}
推荐阅读
- javascript - 如何正确擦除 React 状态数组中的元素?
- r - geom_bar 重叠标签
- node.js - 管道数据从可写到可读
- python - 这个 for 循环和 "new = ' ' " 是如何工作的?
- sql - Rails - 如何查找具有最喜欢给定字符串的 FIELD 记录
- winapi - WinApi 中的 SetWindowSubclass 函数
- javascript - 如何让 Prettier 在 vim 中使用 js 文件?
- python - 如何从 astropy erfa 中获取 WGS84 轴
- c# - 实体框架 6.0 SQL-first 与没有 .edmx 的数据库同步?
- vb.net - 从 Panel 访问 Label 对象,全部在 ToolStripMenuItem 内