我正在尝试使用dataweave可视映射器将名称(从db返回)映射为名字和姓氏,因为我需要拆分字符串(name),我正在使用dataweave的splitBy()函数splitBy(String,String) :数组,如您所见,它返回字符串数组,我想对名字键示例输入打个招呼:firstName:“ hello world” splitBy(“”)示例输出:firstname:[“ hello”,“ world”] ,
mycode:
%dw 2.0 output application/json --- payload map ( payload01 , indexOfPayload01 ) -> { id: payload01.accountID default "", firstName: "payload01.name" splitBy(" "), address: ((payload01.street default "") ++ (payload01.city default "")) ++ (payload01.state default ""), postal: payload01.postal default "", country: payload01.country default "", creationDate: payload01.creationDate as String default "", accountType: payload01.accountType default "", miles: payload01.miles default 0 }
更新:我通过在dataweave中实现了这一点,方法是制作一个用户定义的函数,该函数调用splitBy()函数。在下面的代码中,我解析名字和姓氏从db返回的名称
%dw 2.0 output application/json fun split(data) = data splitBy (" ") --- payload map ( payload01 , indexOfPayload01 ) -> { id: payload01.accountID default "", address: ((payload01.street default "") ++ (payload01.city default "")) ++ (payload01.state default ""), postal: payload01.postal default "", country: payload01.country default "", creationDate: payload01.creationDate as String default "", accountType: payload01.accountType default "", firstName: split(payload01.name)[0], lastName: split(payload01.name)[1], miles: payload01.miles default 0 }
小智.. 5
听起来您只需要选择结果数组中的第一项即可:
firstName : (payload01.name splitBy " ")[0]
听起来您只需要选择结果数组中的第一项即可:
firstName : (payload01.name splitBy " ")[0]