0

I have a json response received from API Call the sample response is something like this

{
 "meta": {
   "code": "200"
  },
 "data": [
   {
    "Id": 44,
    "Name": "Malgudi ABC"
  },
  {
    "Id": 45,
    "Name": "Malgudi, DEF"
  }
]
}

I am trying to make List of Object from it, the code that i've written for this is

private static List<TPDetails> getListOfTpDetails(ResponseEntity<?> responseEntity){
       ObjectMapper objectMapper = new ObjectMapper();
        List<TPDetails> tpDetailsList = objectMapper.convertValue(responseEntity.getBody().getClass(), new TypeReference<TPDetails>(){});
        return tpDetailsList;
    }

Where TPDetails Object is Like this

public class TPDetails {
    int Id;
    String Name;
}

the code which i have used is resulting in

java.lang.IllegalArgumentException: Unrecognized field "meta" (class com.sbo.abc.model.TPDetails), not marked as ignorable (2 known properties: "Id", "Name"])
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.sbo.abc.model.TPDetails["meta"])

I want to convert the Above JSON response in List

List<TPDetails> abc = [
{"Id": 44, "Name": "Malgudi ABC"},
{"Id": 45,"Name": "Malgudi DEF"}
]

Any help would be highly appreciable.Thanks well in advance

1
  • What libraries are you wanting to use, or do you have no preference? Commented Mar 29, 2019 at 10:34

3 Answers 3

1

Create 2 more classes like

public class Temp {
    Meta meta;
    List<TPDetails> data;
}

public class Meta {
    String code;
}

and now convert this json to Temp class.

Temp temp = objectMapper.convertValue(responseEntity.getBody().getClass(), new TypeReference<Temp>(){});

UPDATED :

Make sure responseEntity.getBody() return the exact Json String which you mentioned above.

Temp temp = objectMapper.readValue(responseEntity.getBody(), new TypeReference<Temp>(){});
Sign up to request clarification or add additional context in comments.

3 Comments

java.lang.IllegalArgumentException: Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.sbo.abc.model.TPWrapper["data"]) null, is what i'm getting while parsing the data
@NTanwar use responseEntity.getBody() instead of responseEntity.getBody().getClass(). and are you sure convertValue is the function you want to use. I think readValue is the function
Cannot resolve method 'readValue(capture<?>, anonymous com.fasterxml.jackson.core.type.TypeReference<com.sbo.abc.model.TPWrapper>)' is what i am getting TPWrapper class is like this ``` @Data @JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder({ "meta", "data" }) public class TPWrapper { @JsonIgnoreProperties("meta") TPMeta meta; @JsonProperty("data") List<TPDetails> data; } ```
0

The format of your java class does not reflect the json you are parsing. I think it should be:

class Response {
    Meta meta;
    List<TPDetails> data;
}

class Meta {
    String code;
}

You should then pass Response to your TypeReference: new TypeReference<Response>(){}

If you don't care about the meta field, you can add @JsonIgnoreProperties to your response class and get rid of the Meta class and field.

4 Comments

java.lang.IllegalArgumentException: Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.sbo.abc.model.TPWrapper["data"]) null is something which i have got
@NTanwar do you have getters, setters and empty constructor on the classes?
@Data @JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder({ "meta", "data" }) public class TPWrapper { @JsonIgnoreProperties("meta") TPMeta meta; @JsonProperty("data") List<TPDetails> data; } i am using lombok and annotations, so need not to write it explicitly
I have noticed now, why are you using the convertValue method instead of the readValue? Also, I have added this configuration to the objectmapper and it working properly: objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
0

Create/update following class, I am storing JSON file, since do not have service, but should be fine and Able to parse it and read it from the following model.

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "meta",
        "data"
})
public class OuterPoJo {

    @JsonProperty("meta")
    private Meta meta;
    @JsonProperty("data")
    private List<TPDetails> data = null;

    @JsonProperty("meta")
    public Meta getMeta() {
        return meta;
    }

    @JsonProperty("meta")
    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    @JsonProperty("data")
    public List<TPDetails> getData() {
        return data;
    }

    @JsonProperty("data")
    public void setData(List<TPDetails> data) {
        this.data = data;
    }
}

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "code"
})
public class Meta {

    @JsonProperty("code")
    private String code;

    @JsonProperty("code")
    public String getCode() {
        return code;
    }

    @JsonProperty("code")
    public void setCode(String code) {
        this.code = code;
    }

}

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "Id",
        "Name"
})
public class TPDetails {

    @JsonProperty("Id")
    private Integer id;
    @JsonProperty("Name")
    private String name;

    @JsonProperty("Id")
    public Integer getId() {
        return id;
    }

    @JsonProperty("Id")
    public void setId(Integer id) {
        this.id = id;
    }

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

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

}


import java.io.File;
public class App {
public static void main(String[] args) throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    OuterPoJo myPoJo = objectMapper.readValue(
            new File("file.json"),
            OuterPoJo.class);

    for (TPDetails item : myPoJo.getData()) {
        System.out.println(item.getId() + ":" + item.getName());
    }

}
}

output:

44:Malgudi ABC
45:Malgudi, DEF

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.