1

I have to create Java object from JSON string received in servlet Below is the JSON

[{"name":"name","value":"Shital"},{"name":"email","value":"swankhade@gmail.com"},{"name":"contactno","value":"9920042776"},{"name":"Address","value":"a6 102 Elementa"}]

I tried to change the JSON that is by replacing [ by { and ] by } but it gives some other error. My jackson code where I am getting exception is

// 2. initiate jackson mapper
                ObjectMapper mapper = new ObjectMapper();
// 3. Convert received JSON to Article
                Enrole enrole = mapper.readValue(json, Enrole.class);

And the Enroll class is simple bean class with setter and getter

public class Enrole {
    private String name;
    private String email;
    private long contactno;
    private String address;
double-beep
  • 5,031
  • 17
  • 33
  • 41
Shital
  • 11
  • 1
  • Take a look at [Using Jackson to deserialize into a Map](https://stackoverflow.com/questions/20105723/using-jackson-to-deserialize-into-a-map/20109694#20109694), [Parsing deeply nested JSON properties with Jackson](https://stackoverflow.com/questions/57978790/parsing-deeply-nested-json-properties-with-jackson/57979719#57979719). You need to create list type: `CollectionType collType = mapper.getTypeFactory().constructCollectionType(List.class, Enrole.class)` and use it in `readValue` method: `List users = mapper.convertValue(nodes, collType);` – Michał Ziober Oct 15 '19 at 08:44

1 Answers1

0

This is one of the way

try {
            ObjectMapper mapper = new ObjectMapper();
            String json = "[{\"name\":\"name\",\"value\":\"Shital\"},{\"name\":\"email\",\"value\":\"swankhade@gmail.com\"},{\"name\":\"contactno\",\"value\":\"9920042776\"},{\"name\":\"Address\",\"value\":\"a6 102 Elementa\"}]";
            KeyValue[] jsonObjArr = mapper.readValue(json, KeyValue[].class);
            Enrole enrol = new Enrole();
            for (int i = 0; i < jsonObjArr.length; i++) {

                KeyValue keyVal = jsonObjArr[i];
                if ("name".equals(keyVal.getName())) {
                    enrol.setName(keyVal.getValue());
                }
                if ("email".equals(keyVal.getName())) {
                    enrol.setEmail(keyVal.getValue());
                }
                if ("contactno".equals(keyVal.getName())) {
                    enrol.setContactno(Long.parseLong(keyVal.getValue()));
                }
                if ("address".equals(keyVal.getName())) {
                    enrol.setAddress(keyVal.getValue());
                }
            }
            System.out.println(enrol.getName());
            System.out.println(enrol.getContactno());
            System.out.println(enrol.getAddress());
            System.out.println(enrol.getEmail());
        } catch (Exception e) {
            System.out.println("Exception " + e);
        }

Class with Key and Value :

class KeyValue {
    private String name;
    private String value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

Model Class

class Enrole {
    private String name;
    private String email;
    private long contactno;
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public long getContactno() {
        return contactno;
    }

    public void setContactno(long contactno) {
        this.contactno = contactno;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
Jagadesh
  • 2,104
  • 1
  • 16
  • 28