如何在 Mule 4 中动态设置 JSON 字段名称
How to dynamically set JSON field name in Mule 4
JSON 从 API 收到。
{
"success": true,
"timestamp": 1645988822,
"base": "EUR",
"date": "2022-02-27",
"rates": {
"AED": 4.140586,
"AFN": 102.662987,
"ALL": 121.380824,
"AMD": 538.7856,
"ANG": 2.016644,
"AOA": 559.803561
}
}
我在数据编织表达式中通过这种方式解析 JSON 但它给出了错误
%dw 2.0
output application/json
---
{
"Result":
{
"Data":payload.rates.{Currency} // Currency=AED
}
}
期望的JSON输出应该如下
{
"Result":
{
"Data":4.140586
}
}
您使用的语法不正确。 payload.rates
是一个对象,所以你可以只使用 dynamic selector:
%dw 2.0
output application/json
var selectedCurrency="AED"
---
{
"Result":
{
"Data": payload.rates[selectedCurrency]
}
}
输出:
{
"Result": {
"Data": 4.140586
}
}
试试下面
输入:
{
"success": true,
"timestamp": 1645988822,
"base": "EUR",
"date": "2022-02-27",
"rates": {
"AED": 4.140586,
"AFN": 102.662987,
"ALL": 121.380824,
"AMD": 538.7856,
"ANG": 2.016644,
"AOA": 559.803561
}
}
数据编织
%dw 2.0
output application/json
---
{
"Result":
{
"Data":payload.rates.AED
}
}
输出:
{
"Result": {
"Data": 4.140586
}
}
JSON 从 API 收到。
{
"success": true,
"timestamp": 1645988822,
"base": "EUR",
"date": "2022-02-27",
"rates": {
"AED": 4.140586,
"AFN": 102.662987,
"ALL": 121.380824,
"AMD": 538.7856,
"ANG": 2.016644,
"AOA": 559.803561
}
}
我在数据编织表达式中通过这种方式解析 JSON 但它给出了错误
%dw 2.0
output application/json
---
{
"Result":
{
"Data":payload.rates.{Currency} // Currency=AED
}
}
期望的JSON输出应该如下
{
"Result":
{
"Data":4.140586
}
}
您使用的语法不正确。 payload.rates
是一个对象,所以你可以只使用 dynamic selector:
%dw 2.0
output application/json
var selectedCurrency="AED"
---
{
"Result":
{
"Data": payload.rates[selectedCurrency]
}
}
输出:
{
"Result": {
"Data": 4.140586
}
}
试试下面
输入:
{
"success": true,
"timestamp": 1645988822,
"base": "EUR",
"date": "2022-02-27",
"rates": {
"AED": 4.140586,
"AFN": 102.662987,
"ALL": 121.380824,
"AMD": 538.7856,
"ANG": 2.016644,
"AOA": 559.803561
}
}
数据编织
%dw 2.0
output application/json
---
{
"Result":
{
"Data":payload.rates.AED
}
}
输出:
{
"Result": {
"Data": 4.140586
}
}