1

I'm having trouble in thinking up a way of parsing this type of JSON. I need to be able to access both data and message separately after getting this response inside my application.

{
    "data": [
        {
            "email": "eskaferas@gmail.com",
            "firstName": "Seras",
            "lastName": "Meras"
        },
        {
            "email": "Soras@gmail.com",
            "firstName": "Oras",
            "lastName": "Moras"
        },
        {
            "email": "bzbzb@gmail.com",
            "firstName": "hello",
            "lastName": "bye"
        },
        {
            "email": "lrc@gmail.com",
            "firstName": "Seras",
            "lastName": "Meras"
        }
    ],
    "message": "Success"
}

Could anyone suggest a method specifically for Spring boot? Or a common way that is used in Spring boot to parse this type of JSON.

Thank you.

EDIT

My main questions would be just what tool can be used for Spring Boot and what would my POJO class look like if I used Jackson. Would it be something like this?:

public class testPojo {
  Users[] users;
  String message;
}
  • Can you share you code so far? – akortex Apr 18 '19 at 13:46
  • 2
    Have you tried anything? Have you gone through any documentation or samples provided by Spring official website? – Abhijith Nagarajan Apr 18 '19 at 13:47
  • Write 'How to parse JSON java' in Google please. – Airwavezx Apr 18 '19 at 13:52
  • So far I only have my own service that communicates with different responses which I did not need to parse. This is the response I'm getting from another REST service I'm connecting to and just having trouble of thinking up with a way to parse it to an object or objects. I have only checked other questions here, but did not find a way for this type of JSON. I'm just not even sure where to start. Also, if possible I would need not to use any external JAR files. I can use maven dependencies. – Svajunas Kavaliauskas Apr 18 '19 at 13:52
  • 1
    If you are trying to have someone else do this for you, then you should learn to solve your own problems. This can be quickly solved by a simple google search. – Mike Tung Apr 18 '19 at 13:53
  • @Abhijith Nagarajan The main question for me is if I use Jackson in this instance how would my POJO class would look like? Would it just be an array of users and a string message? – Svajunas Kavaliauskas Apr 18 '19 at 14:01
  • @SvajunasKavaliauskas as Mike Tung said, do a google search before posting a question, here is a [guide](https://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/). If you face problems come back with a question regarding the problem that you have. – Dimitris Apr 18 '19 at 14:15
  • 1
    @Dimitris Alright, thank you I will come back if I run into any problems with specific code. – Svajunas Kavaliauskas Apr 18 '19 at 14:17

2 Answers2

2

Pojo class will look like:

public class TestPojo{

    private Users[] data;
    private String message;

    public Users[] getData() {
        return data;
    }

    public void setData(Users[] data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
     }
}

class Users {
    private String email;
    private String firstName;
    private String lastName;
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

use @RequestBody in your controller's method parameter.

@RestController
class AbcController{

    PostMapping("/api")
    public String create(@RequestBody TestPojo test){
    // you can acess your json in test object
    return "done"
    } 
}
Bishal Jaiswal
  • 1,684
  • 13
  • 15
1
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.Getter;
import lombok.Setter;
import org.junit.Test;
import org.springframework.context.annotation.Bean;

import java.io.IOException;
import java.util.List;

public class TestJUnit {

    @Test
    public void exec() {
       String json = "{" +
               "    \"data\": [" +
               "        {" +
               "            \"email\": \"eskaferas@gmail.com\"," +
               "            \"firstName\": \"Seras\"," +
               "            \"lastName\": \"Meras\"" +
               "        }," +
               "        {" +
               "            \"email\": \"Soras@gmail.com\"," +
               "            \"firstName\": \"Oras\"," +
               "            \"lastName\": \"Moras\"" +
               "        }," +
               "        {" +
               "            \"email\": \"bzbzb@gmail.com\"," +
               "            \"firstName\": \"hello\"," +
               "            \"lastName\": \"bye\"" +
               "        }," +
               "        {" +
               "            \"email\": \"lrc@gmail.com\"," +
               "            \"firstName\": \"Seras\"," +
               "            \"lastName\": \"Meras\"" +
               "        }" +
               "    ]," +
               "    \"message\": \"Success\"" +
               "}".replaceAll(" ", "");

        ObjectMapper mapper = new ObjectMapper();

        try {
            JsonParser parser = mapper.readValue(json, JsonParser.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Getter
    @Setter
    private static class JsonParser {
        private List<Data> data;
        private String message;
    }

    @Getter
    @Setter
    private static class Data{
        private String email;
        private String firstName;
        private String lastName;
    }

//    @Configuration
    private static class Configuration{
    @Bean
    public ObjectMapper objectMapper(){
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        mapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
        mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
        mapper.setVisibility(mapper.getSerializationConfig()
                .getDefaultVisibilityChecker().withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
        mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
        mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
        return mapper;
    }
    }
}
Dev_java
  • 11
  • 2
  • Thank you. How could I then serialise it back from my object to Json String? To get the same string as the starting one. – Svajunas Kavaliauskas Apr 18 '19 at 14:31
  • https://stackoverflow.com/questions/55728274/why-does-json-object-comes-before-json-values-in-below/55729156?noredirect=1#comment98137568_55729156 – Dev_java Apr 18 '19 at 14:57