2016-03-28 4 views
0

die umwandeln Lassen Sie sagen, ich einen Anruf an einen thrid Partei-API ein Objekt Aufgabe zu bekommen und ich die folgende JSON String im Gegenzug:Wie JSON-Objekt von Dritter api in lokale POJO

{ 
    "tasks": [ 
     { 
     "id": 1, 
     "code": "CODE", 
     "description": "Dummy Task", 
     "withConfirmation": false, 
     "resource": { 
      "id": "abcdef12-fe14-57c4-acb5-1234e7456d62", 
      "group": "Doctor", 
      "firstname": "Toto", 
      "lastname": "Wallace", 
     }, 
     { 
     "id": 2, 
     "code": "CODE", 
     "description": "Dummyyy Taaask", 
     "withConfirmation": false 
     } 
    ] 
} 

In der zurückgegebenen JSON haben wir eine Aufgabe, die mit einer Ressource verbunden werden kann.

In unserem System eine Aufgabe ist wie folgt:

@JsonAutoDetect 
public class Task implements Serializable { 

    private Integer id; 
    private String code = "BASIC"; 
    private String description; 
    private boolean withConfirmation = false; 


    /** 
    * CONSTRUCTOR 
    */ 
    public Task() { 
    } 
    public Integer getId() { 
     return id; 
    } 

    @JsonProperty 
    public String getCode() { 
     return code; 
    } 
    public void setCode(String code) { 
     this.code = code; 
    } 

    @JsonProperty 
    public String getDescription() { 
     return description; 
    } 
    public void setDescription(String description) { 
     this.description = description; 
    } 

    @JsonProperty 
    public boolean isWithConfirmation() { 
     return withConfirmation; 
    } 
    public void setWithConfirmation(boolean withConfirmation) { 
     this.withConfirmation = withConfirmation; 
    } 

    public String toString() {... 
    } 
} 

und eine Ressource sieht wie folgt aus:

public class Resource implements Serializable { 
    ... 

    private String firstname; 
    private String lastname; 
    private MedicalGroup group; // id + name + description 
    private Set<Task> tasks = new HashSet<Task>(0); 
    ... 
    // getters and setters and toString etc. 
    ... 
} 

So ist der große Unterschied, abgesehen von den Feldnamen ist das eine Aufgabe enthält keine Ressource aber die Beziehung ist eher im Gegensatz iter Richtung, was bedeutet, dass eine Ressource kann nAufgabe halten.

Was wäre für diesen Fall der beste Weg zu serialisieren das zurückgegebene JSON-Objekt von der dritten Partei und konvertieren/zu einem Pojo aus meinem eigenen System zu konvertieren? Ich lese gerade Gson Doc, um es zu versuchen, aber jeder Vorschlag ist willkommen. Dieser Code muss leicht wiederverwendbar sein, da er in mehreren Projekten benötigt wird.

+0

Haben wir ein Verbindungsfeld zwischen Ressource und Aufgabe oder wir müssen introspect JSON Antwort um herauszufinden, welche Aufgabe welche Ressource hält? –

+0

wir müssen inspizieren, um es herauszufinden – jon

+0

Ein Beispiel dafür http://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/ – Naruto

Antwort

1

Es ist nicht voll funktionierenden Code, weil ich keine Ahnung, wie man mit Resource arbeiten möchten. Sollte Json eine neue Ressource erstellen oder versuchen, eine bereits vorhandene zu finden? Wie werden Sie MedicalGroup von json erstellen, weil es dafür keine Daten gibt. Ich würde dies in Kommentaren fragen, aber es ist nicht genug Platz. Und hier ist eine Demo, wie Sie versuchen können, die meisten Probleme außer der Resources to/from json Zuordnung zu lösen.

Hauptidee ist @JsonAnyGetter public Map<String, Object> getAdditionalProperties() und @JsonAnySetter public void setAdditionalProperty(String name, Resource value) in Ihrem Task POJO hinzuzufügen:

@JsonAnyGetter 
    public Map<String, Object> getAdditionalProperties() { 

     HashMap<String, Object> map= new HashMap<>(); 

     // IMPORTANT 
     // here we can try to find resource that has this task 
     // and export its info to json like this: 


     // CHANGE THIS 
     Resource res = new Resource(); 
     res.firstname = "Toto"; 
     res.lastname = "Wallace"; 

     // IMPORTANT END 

     map.put("resource", res); 

     return map; 
    } 

    @JsonAnySetter 
    public void setAdditionalProperty(String name, Resource value) { 
     // IMPORTANT 
     // Here you have to create or find appropriate Resource in your code 
     // and add current task to it 
     System.out.println(name+" "+ value); 
    } 

FULL Demo:

import com.fasterxml.jackson.annotation.*; 
import com.fasterxml.jackson.databind.ObjectMapper; 

import java.io.IOException; 
import java.io.Serializable; 
import java.util.*; 

public class Main3 { 
private static String json = "{\n" + 
     " \"tasks\": [\n" + 
     "  {\n" + 
     "  \"id\": 1,\n" + 
     "  \"code\": \"CODE\",\n" + 
     "  \"description\": \"Dummy Task\",\n" + 
     "  \"withConfirmation\": false,\n" + 
     "  \"resource\": {\n" + 
     "   \"id\": \"abcdef12-fe14-57c4-acb5-1234e7456d62\",\n" + 
     "   \"group\": \"Doctor\",\n" + 
     "   \"firstname\": \"Toto\",\n" + 
     "   \"lastname\": \"Wallace\"\n" + 
     "  }},\n" + 
     "  {\n" + 
     "  \"id\": 2,\n" + 
     "  \"code\": \"CODE\",\n" + 
     "  \"description\": \"Dummyyy Taaask\",\n" + 
     "  \"withConfirmation\": false\n" + 
     "  }\n" + 
     " ]\n" + 
     " }"; 

public static void main(String[] args) throws IOException { 
    ObjectMapper mapper = new ObjectMapper(); 
    TasksList tl = mapper.readValue(json, TasksList.class); 
    String result = mapper.writeValueAsString(tl); 
    System.out.println(result); 
} 

private static class TasksList { 
    @JsonProperty(value = "tasks") 
    private List<Task> tasks; 
} 
@JsonIgnoreProperties(ignoreUnknown = true) 
public static class Resource implements Serializable { 
    @JsonProperty(value = "firstname") 
    private String firstname; 
    @JsonProperty(value = "lastname") 
    private String lastname; 

    // HAVE NO IDEA HOW YOU GONNA MAP THIS TO JSON 
    // private MedicalGroup group; // id + name + description 
    private Set<Task> tasks = new HashSet<Task>(0); 

    @Override 
    public String toString() { 
     return "Resource{" + 
       "firstname='" + firstname + '\'' + 
       ", lastname='" + lastname + '\'' + 
       ", tasks=" + tasks + 
       '}'; 
    } 
} 

@JsonAutoDetect 
public static class Task implements Serializable { 

    private Integer id; 
    private String code = "BASIC"; 
    private String description; 
    private boolean withConfirmation = false; 

    /** 
    * CONSTRUCTOR 
    */ 
    public Task() { 
    } 
    public Integer getId() { 
     return id; 
    } 

    @JsonProperty 
    public String getCode() { 
     return code; 
    } 
    public void setCode(String code) { 
     this.code = code; 
    } 

    @JsonProperty 
    public String getDescription() { 
     return description; 
    } 
    public void setDescription(String description) { 
     this.description = description; 
    } 

    @JsonProperty 
    public boolean isWithConfirmation() { 
     return withConfirmation; 
    } 
    public void setWithConfirmation(boolean withConfirmation) { 
     this.withConfirmation = withConfirmation; 
    } 

    @JsonAnyGetter 
    public Map<String, Object> getAdditionalProperties() { 

     HashMap<String, Object> map= new HashMap<>(); 

     // IMPORTANT 
     // here we can try to find resource that has this task 
     // and export its info to json like this: 
     // CHANGE THIS 

     Resource res = new Resource(); 
     res.firstname = "Toto"; 
     res.lastname = "Wallace"; 

     // IMPORTANT END 

     map.put("resource", res); 

     return map; 
    } 

    @JsonAnySetter 
    public void setAdditionalProperty(String name, Resource value) { 
     // IMPORTANT 
     // Probably here you have to create or find appropriate Resource in your code 
     // and add current task to it 
     System.out.println(name+" "+ value); 
    } 

    @Override 
    public String toString() { 
     return "Task{" + 
       "id=" + id + 
       ", code='" + code + '\'' + 
       ", description='" + description + '\'' + 
       ", withConfirmation=" + withConfirmation + 
       '}'; 
    } 
} 
} 
+0

Nur um Sie wissen zu lassen, werde ich Ihre Lösung so schnell wie möglich testen und Ihnen sofort ein Feedback geben.Es kann manchmal schwierig sein, sich auf eine einzelne Aufgabe zu konzentrieren ... – jon

0

können Sie Bibliothek von Google verwenden, um Json in Pojo Class zu konvertieren.

new Gson().fromJson(jsonString,Response.class);