2017-07-04 57 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