2017-07-04 55 views
-1

我得到有效载荷在下面的java格式:如何在mule中使用java映射?

[ 
     {emai:'[email protected]',id:a1}, 
     {emai:'[email protected]',id:a2}, 
     {emai:'[email protected]',id:a3}, 
     .... 
] 

上述输入i被发送到Java组件,其它需要给输出在下面格式

[ 
     {emai:'[email protected]',id:[a1,a2]}, 
     {emai:'[email protected]',id:a3}, 
     .... 
] 

即我需要所有的ID结合将电子邮件转换为单个数组。

任何人都可以共享链接来编写java代码吗?或任何示例代码?

谢谢..,

+0

有效载荷是JSON对象吗?有效载荷是 –

+0

,对象的集合。有效载荷是来自salesforce select查询的响应,即从联系人中选择电子邮件,ID – Thiru

+0

您使用哪种数据类型来存储它?帮助我,这样我可以帮助你的代码。 –

回答

1

无关,与骡子可能。假设你有一个类InputOutput代表您相应列表元素,你可以这样写:

public static class Output { 
    private String email; 
    private List<String> id; 

    public Output(String email, List<String> id) { 
     this.email = email; 
     this.id = id; 
    } 
    // .. getters 
} 

public static class Input { 
    private String email; 
    private String id; 

    public Input(String email, String id) { 
     this.email = email; 
     this.id = id; 
    } 
    // .. getters 
} 

@Test 
public void test() { 
    List<Input> inputs = Arrays.asList(
      new Input("[email protected]", "a1"), 
      new Input("[email protected]", "a2"), 
      new Input("[email protected]", "a3") 
    ); 

    List<Output> results = inputs.stream() 
      .collect(Collectors.groupingBy(Input::getEmail)) 
      .entrySet().stream().map(e-> 
        new Output(e.getKey(), 
          e.getValue().stream().map(Input::getId).collect(Collectors.toList()) 
        ) 
      ).collect(Collectors.toList()); 

    System.out.println(results); 
} 

而且你的输入列表将被转换为所需的格式。我假设你可以想出如何将你发送和接收的任何格式转换为Mule中的POJO。

0
import org.json.simple.JSONObject; 
import org.json.simple.JSONArray; 
import org.json.simple.parser.ParseException; 
import org.json.simple.parser.JSONParser; 

private final static String s = "[{emai:'[email protected]',id:a1},{emai:'[email protected]',id:a2},{emai:'[email protected]',id:a3},....]"; 

public static void main(final String[] argv) throws JSONException { 
Object obj = parser.parse(s); 
JSONArray array = (JSONArray)obj; 
//Turn the payload into a JSONArray 
JSONArray new_arry=new JSONArray(); 
int payload_length = array.length(); 
ArrayList<String> emails = new ArrayList(); 
for(int i=0;i<payload_length;i++){ 
//For every email, use combine to add ids into a JSONArray and attach it to 
//a single email 
String email = array.getJSONObject(i).get("email").toString(); 
//If email has been already considered, skip the check for it 
if(array.contains(email)) 
continue; 
else{ 
emails.add(email); 
JSONArray combine = new JSONArray(); 
for(j=i;j<payload_length;j++){ 
if(array.get(j).get("email").toString()==email) 
combine.put(array.get(j).get("id").toString(); 
    } 
new_array.put(new JSONObject().(email,combine); 
    } 
} 
1

你可以试试下面的代码dataweave

%dw 1.0 
%output application/java 
--- 
payload groupBy $.emai map { 
    emai : $.emai[0], 
    id : $.id 
} 

希望这有助于。

+0

嗨,在这种情况下,我收到一些电子邮件字段也为空,我越来越“不能胁迫a:null为a:字符串。”错误 – Thiru

+0

将过滤器放在有效负载上以检查有效数据,然后映射它。请检查更多关于过滤器的细节[这里](https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#filter) – AnupamBhusari