2015-06-04 68 views
0

我用JsonDeserializer反序列化傑克遜在春季啓動

public class CustomJacksonDeserialize extends JsonDeserializer<Activity> { 
    @Override 
    public Activity deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws 
      IOException, NullPointerException { 
     ObjectMapper objectMapper = new ObjectMapper(); 
     JsonNode actualObj = objectMapper.readTree(jsonParser.getValueAsString()); 

      return new Activity(actualObj.get("name").asText()); 
    } 
} 

這裏呼籲RestTemplate

{"results":[{"testing":{"name":"soham"}},{"testing":{"firstname":"john","lastname":"don"}}]} 

現在我解析數據得到兩種類型的JSON的是我班

@JsonDeserialize(using = CustomJacksonDeserialize.class) 
public class Activity { 

    private String name; 

    public Activity(String name) { 
     this.name = name; 
    } 
} 

現在我的問題是如何解析這個JSON。 @JsonDeserialize在這種情況下沒有幫助。任何其他選項?或者在這種情況下如何使用@JsonDeserialize

回答

0

您可以修改CustomJacksonDeserialize這樣的:

class CustomJacksonDeserialize extends JsonDeserializer<Activity> { 
    @Override 
    public Activity deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, 
      NullPointerException { 

     JsonNode jsonNode = jsonParser.readValueAsTree(); 

     String name=jsonNode.get("results").get(0).get("testing").get("name").asText(); 

     return new Activity(name); 
    } 
} 

方法二: 或者如果您也可以去serilaize使用對象映射器本身這並不需要CustomJacksonDeserialize這個JSON:

String jsonString = "{\"results\":[{\"testing\":{\"name\":\"soham\"}},{\"testing\":{\"firstname\":\"john\",\"lastname\":\"don\"}}]}"; 
    ObjectMapper objectMapper = new ObjectMapper(); 

JsonNode jsonNode = objectMapper.readTree(jsonString); 
JsonNode resultNode=jsonNode.get("results"); 
String name = resultNode.get(0).get("testing").get("name").asText(); 

Activity activity=new Activity(name); 

String firstName= resultNode.get(1).get("testing").get("firstname").asText(); 
String lastName= resultNode.get(1).get("testing").get("lastname").asText(); 

Activity2 activity2=new Activity2(firstName, lastName); 

備註:我提取的值爲name,firstname和給定JSON的lastname,您可以相應地修改此邏輯。

第三條道路:在這裏,我可以通過JSON數組迭代,將同時創建活動的單獨的對象:

String jsonString = "{\"results\":[{\"testing\":{\"name\":\"soham\"}},{\"testing\":{\"firstname\":\"john\",\"lastname\":\"don\"}}]}"; 

     ObjectMapper objectMapper = new ObjectMapper(); 

     JsonNode jsonNode = objectMapper.readTree(jsonString); 

     JsonNode resultNode = jsonNode.get("results"); 

     List<Activity> activityList1 = new ArrayList<Activity>(); 

     List<Activity2> activityList2 = new ArrayList<Activity2>(); 

     for (int i = 0; i < resultNode.size(); i++) { 

      JsonNode testingNode = resultNode.get(i).get("testing"); 

      if (testingNode.has("name")) { 
       String name = testingNode.get("name").asText(); 

       Activity activity = new Activity(name); 
       activityList1.add(activity); 

      } 

      if (testingNode.has("firstname")) { 
       String firstName = testingNode.get("firstname").asText(); 
       String lastName = testingNode.get("lastname").asText(); 

       Activity2 activity2 = new Activity2(firstName, lastName); 
       activityList2.add(activity2); 

      } 

     } 
+0

我想將名字和姓氏也以不同的POJO類? – Soham

+0

你可以爲另一個類創建一個解串器,並且在這個解串器中你可以返回另一個POJO類的實例, –

+0

@Soham我已經更新了我的答案,我認爲第二種方法更符合你的要求。 –