2017-07-04 7 views
-1

i erhalten Nutzlast in unter Java-Format:Wie Java Mapping in Maultier zu verwenden?

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

die oben Eingang i zu Java-Komponente gesendet wird, die die Ausgabe im folgenden Format geben, müssen

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

dh ich alle id kombinieren müssen einer E-Mail in ein einzelnes Array.

kann jemand den Link teilen, um Java-Code zu schreiben? oder irgendeinen Beispielcode?

Danke ..,

+0

Ist die Nutzlast ein JSON-Objekt? –

+0

Nutzlast ist, Sammlung von Objekten. Payload ist eine Antwort von Salesforce Select Query, d. h. E-Mail, ID aus Kontakten auswählen – Thiru

+0

Welche Art von Datentyp verwenden Sie, um das zu speichern? Hilf mir, damit ich dir mit dem Code helfen kann. –

Antwort

1

Nichts zu tun mit Mule wahrscheinlich. Angenommen, Sie haben eine Klasse Input und Output Ihre entsprechenden Listenelemente darstellen Sie schreiben können:

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); 
} 

Und Ihre Eingabeliste wird in das gewünschte Format konvertiert werden. Ich nehme an, Sie können herausfinden, wie Sie das Format, das Sie senden und empfangen, an POJOs in Mule umwandeln.

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

können Sie versuchen, folgende dataweave Code

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

Hoffnung, das hilft.

+0

Hallo, in diesem Fall bekomme ich einige E-Mail-Felder als auch leer, ich bekomme "Kann nicht zwingen: NULL zu a: string." Fehler – Thiru

+0

Filter auf Payload setzen, um gültige Daten zu überprüfen und dann zuzuordnen. Bitte überprüfen Sie weitere Details zum Filter [hier] (https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#filter) – AnupamBhusari