-3

I have a String with the following value:

{
  "keyA": {
    "ID": "123",
    "Name": "TESTA",
    "Mobile": "1111"
  },
  "keyB": {
    "ID": "456",
    "Name": "TESTB",
    "Mobile": "2222"
  }
}

How to convert json to list and class formats

And I want to parse it to a List

Please advise how to achieve using Jackson ObjectMapper?

Senthil RS
  • 271
  • 3
  • 8
  • 17
  • 1
    Possible duplicate of [How to parse a JSON string to an array using Jackson](http://stackoverflow.com/questions/7246157/how-to-parse-a-json-string-to-an-array-using-jackson) – epoch Apr 07 '17 at 09:25
  • No duplicate, The Json values are different formats – Senthil RS Apr 07 '17 at 09:30
  • You will need to create a class for keyA then. i.e `class Key` with the attributes specified. – epoch Apr 07 '17 at 09:34
  • Class Key as super class with KeyA and KeyB as objects belong to class key? so total create 3 class? – Senthil RS Apr 07 '17 at 09:36
  • no, just one; the fields are the same. `keyA` and `keyB` are just the field names – epoch Apr 07 '17 at 09:37
  • eg: private String keyA //getters and setters But how do i associate object mapper? – Senthil RS Apr 07 '17 at 09:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/140158/discussion-between-senthil-rs-and-epoch). – Senthil RS Apr 07 '17 at 09:39

1 Answers1

1

You could try this for testing:

public static void main(String[] args) {
    String jsonString = "{\n" +
                "  \"keyA\": {\n" +
                "    \"ID\": \"123\",\n" +
                "    \"Name\": \"TESTA\",\n" +
                "    \"Mobile\": \"1111\"\n" +
                "  },\n" +
                "  \"keyB\": {\n" +
                "    \"ID\": \"456\",\n" +
                "    \"Name\": \"TESTB\",\n" +
                "    \"Mobile\": \"2222\"\n" +
                "  }\n" +
                "}";
    List<CustomObject> customObjects = new ArrayList<CustomObject>();
    ObjectMapper mapper = new ObjectMapper();
    try {
        ClassA myClassAObject= mapper.readValue(jsonString, ClassA.class);
        customObjects.add(myClassAObject.getKeyA());
        customObjects.add(myClassAObject.getKeyB());
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    System.out.println(customObjects.size());
}

My other model classes, CustomObject:

public class CustomObject {

    private String id;
    private String name;
    private String mobile;

    @JsonProperty("ID")
    public String getId() {
        return id;
    }

    @JsonProperty("ID")
    public void setId(String id) {
        this.id = id;
    }

    @JsonProperty("Name")
    public String getName() {
        return name;
    }

    @JsonProperty("Name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("Mobile")
    public String getMobile() {
        return mobile;
    }

    @JsonProperty("Mobile")
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
}

and ClassA

public class ClassA {

    private CustomObject keyA;
    private CustomObject keyB;

    public CustomObject getKeyA() {
        return keyA;
    }

    public void setKeyA(CustomObject keyA) {
        this.keyA = keyA;
    }

    public CustomObject getKeyB() {
        return keyB;
    }

    public void setKeyB(CustomObject keyB) {
        this.keyB = keyB;
    }
}

In case you have issues, this is the library version that I used in Maven:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.1.3</version>
</dependency>
aUserHimself
  • 1,589
  • 2
  • 17
  • 26
  • tried and got error Can not deserialize instance of java.util.ArrayList out of START_OBJECT token – Senthil RS Apr 07 '17 at 09:48
  • @Senthil RS Can you post the full error stack and `ClassA` code? – aUserHimself Apr 07 '17 at 10:09
  • Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: {"keyA":{"ID":"123","Name":"TESTA","Mobile":"1111"}}; line: 1, column: 12] (through reference chain: com.model.ResponseBean["keyA"]) – Senthil RS Apr 07 '17 at 10:23