从Mule ESB中的JSON中提取数组 [英] Extracting array from JSON in mule esb

查看:295
本文介绍了从Mule ESB中的JSON中提取数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mule 3.4 CE,并且有一个通过HTTP以以下格式传输的JSON数据:

I'm using Mule 3.4 CE and I have a JSON data coming through HTTP in the following format:

{
   "People" : [
    {
       "Details" : 
       {
         "Name" : "John Smith",
         "Email" : "abc@mail.com"
       }
    },
    {
       "Details" : 
       {
         "Name" : "Tim Smith",
         "Email" : "def@mail.com"
       }
    },
    {
       "Details" : 
       {
         "Name" : "Ken Smith",
         "Email" : "ghi@mail.com"
       }
    },
}

我需要提取电子邮件并使用这些电子邮件查找Salesforce联系人,与此同时,我想保留JSON有效负载.所以我的问题是如何通过MEL提取电子邮件? (例如,例如"People/Details/*/Email"之类的代码-我知道这是无效的,但是我正在寻找正确的语法.

I need to extract the emails and lookup the Salesforce contact with these emails and at the same time I want to retain the JSON payload. So my question is how do I extract the emails through MEL? (for e.g. something like "People/Details/*/Email" - I know this is not a valid, but I'm looking for the right syntax.

编辑:我想一次性提取电子邮件,而不是编制索引(例如,使用People/Details [0] .Email,可能使用MEL.

Edit: I want to extract the emails in one shot rather than indexing (for e.g. People/Details[0].Email, possibly using MEL.

推荐答案

查询json的最佳方法是将其转换为Map.

There best way to query json is to transform it to a Map.

<json:json-to-object-transformer returnClass="java.util.HashMap" />

然后使用MEL(如标准MVEL或Java语法)进行查询

And then query it using MEL like standard MVEL or Java syntax

<logger message="#[payload.People[0].Details.email]" level="INFO" />

如果您想保持原始的json有效负载不变,则可以使用扩充程序将地图存储在变量中:

If you want to keep the original json payload intact, you can store the map in a variable using an enricher:

<enricher target="#[flowVars.myJsonMap]">
   <json:json-to-object-transformer returnClass="java.util.HashMap" />
</enricher>

并查询变量而不是有效载荷:

And query the variable instead of the payload:

<logger message="#[flowVars.myJsonMap.People[0].Details.email]" level="INFO" />

您还可以使用Jackson将 json 映射到自定义类,并将returnClass属性更改为您的类.

You could also map the json to a custom class using Jackson and change the returnClass attribute to your class.

此MEL备忘单详细介绍了使用MEL进行JSON处理以及如何处理地图,数组等的情况: http://www.mulesoft.org/documentation/display/current/MEL+Cheat+Sheet

This MEL cheat sheet detail JSON processing with MEL and also how to handle Maps, arrays etc: http://www.mulesoft.org/documentation/display/current/MEL+Cheat+Sheet

注意::您可能会遇到#[json:]评估程序,但不赞成使用上述方法.

Note: You may come across a #[json:] evaluator, but this is deprecated in favour of the approach above.

更新:

如果您想一次抓取所有电子邮件,可以使用MVEL投影:

If you want to grab all the emails at once you can use MVEL projections:

<enricher target="#[flowVars.myJsonMap]" source="#[(Details.email in payload.People)]">
       <json:json-to-object-transformer returnClass="java.util.HashMap" />

    </enricher>

动态投影: http://mvel.codehaus.org/MVEL+2.0+Projections+and+折叠

这篇关于从Mule ESB中的JSON中提取数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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