字符串映射到Object / JSON - Mule DataWeave

时间:2016-07-26 00:36:34

标签: mule anypoint-studio dataweave

我有像这样的字符串(看起来像地图)

{键1 =值;键2 =值; KEY3 =值3; ... keyn =值N;}

我想要转换为java对象或像这样的JSON有效负载:

{" KEY1" :" value1"," key2" :" value2"," key3" :" value3",..." keyn" :"重视"}

有骡子的方法吗?我试图避免为此问题编写自定义java类。数据编织的东西可以帮助吗?

2 个答案:

答案 0 :(得分:1)

我在dataweave中使用了以下来操作字符串。

%dw 1.0
%output application/json
---
(payload replace /[{}]/ with "" splitBy ";")  map using (data = $ splitBy "=")   {
    (data[0]) : data[1]
}

输入数据: -

"{key1=value1;key2=value2;key3=value3;keyn=valuen}"

输出: -

[
  {
    "key1": "value1"
  },
  {
    "key2": "value2"
  },
  {
    "key3": "value3"
  },
  {
    "keyn": "valuen"
  }
]

希望这会有所帮助..

答案 1 :(得分:1)

用它来获取地图

%dw 1.0
%output application/json
---
{((payload replace /[{}]/ with "" splitBy ";")  map using (data = $ splitBy "="){
    (data[0]) : data[1]
})}

希望这有帮助。