简体   繁体   中英

Deserialize jackson in spring boot

I am getting two types of json by calling RestTemplate

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

Now I am parsing data using 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());
    }
}

Here is my class

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

    private String name;

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

Now my question is how to parse this json. @JsonDeserialize isn't helpful in this case.Any other options?Or how to use @JsonDeserialize in this case.

You can modify CustomJacksonDeserialize like this :

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

Second Approach: Alternatively If you can also de-serilaize this JSON using Object mapper itself This does not require CustomJacksonDeserialize :

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

NOTE : I have extracted value of name , firstname and lastname for given JSON, you can modify this logic accordingly.

Third Approach: Here I can iterate through the JSON array and will create separate objects of both the activities:

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

            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM