Jackson Jsonmappingexception: Can Not Deserialize Instance

Convert JSON to Object throws JsonMappingException "Can not deserialize instance of class out of START_ARRAY token"

The problem is that you need to deserialize JSON array as java array, or java List.
Here you can find the answer How to use Jackson to deserialise an array of objects.

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

Your Json is not valid. Try validating your json first http://jsonlint.com/

You can try something like

{
"product": [{
"product_type_id": 1,
"product_cat_id": 1,
"product_type_nm": "PED"
}, {
"product_type_id": 2,
"product_cat_id": 2,
"product_type_nm": "MOBILE APP"
}, {
"product_type_id": 3,
"product_cat_id": 1,
"product_type_nm": "MOBILE"
}, {
"product_type_id": 4,
"product_cat_id": 3,
"product_type_nm": "PAYMENT"
}, {
"product_type_id": 5,
"product_cat_id": 5,
"product_type_nm": "USER"
}, {
"product_type_id": 6,
"product_cat_id": 6,
"product_type_nm": "SMS"
}, {
"product_type_id": 9,
"product_cat_id": 6,
"product_type_nm": "EMAIL"
}, {
"product_type_id": 10,
"product_cat_id": 6,
"product_type_nm": "TOPUP"
}]
}

Jackson JsonMappingException: Can not deserialize instance

  1. Remove the Constructor in Entries & Phone

  2. GetEntries results = mapper.readValue(new URL("http://collegewires.com/android/jacksoncw.json"), GetEntries.class);

  3. Entries seems to be a parameter in your JSON.

GetEntries.java

package com.collegewires.jackson;

import java.util.List;

public class GetEntries{
private List<Entries> entries;

public List<Entries> getEntries(){
return this.entries;
}
public void setEntries(List<Entries> entries){
this.entries = entries;
}
}

Entries.java

package com.collegewires.jackson;

import java.util.List;

public class Entries{
private String address;
private String email;
private String gender;
private String id;
private String name;
private Phone phone;

public String getAddress(){
return this.address;
}
public void setAddress(String address){
this.address = address;
}
public String getEmail(){
return this.email;
}
public void setEmail(String email){
this.email = email;
}
public String getGender(){
return this.gender;
}
public void setGender(String gender){
this.gender = gender;
}
public String getId(){
return this.id;
}
public void setId(String id){
this.id = id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public Phone getPhone(){
return this.phone;
}
public void setPhone(Phone phone){
this.phone = phone;
}
}

Phone.java

package com.collegewires.jackson;

import java.util.List;

public class Phone{
private String home;
private String mobile;
private String office;

public String getHome(){
return this.home;
}
public void setHome(String home){
this.home = home;
}
public String getMobile(){
return this.mobile;
}
public void setMobile(String mobile){
this.mobile = mobile;
}
public String getOffice(){
return this.office;
}
public void setOffice(String office){
this.office = office;
}
}

Can not deserialize instance of PaymentResponse[] out of START_OBJECT token

The ideal would be if you could change the response to something like:

[{
"uid": "111",
"name": "kiran"
}, {
"uid": "112",
"name": "kumar"
}
]

If you can't change it, then please try this:

ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
JsonNode array = objectMapper.readValue(anotherResponseStr, JsonNode.class);
JsonNode paymentResponse = array.get("PaymentResponse[]");

List<PaymentResponse> p = objectMapper.readValue(paymentResponse.toString(),
typeFactory.constructCollectionType(List.class, PaymentResponse.class));

Error: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of Entities.Student out of START_ARRAY token

Your method takes one Student as parameter

public Response createAccount(Student aStudent) {

But you are sending an array.

So your method should look like

public Response createAccount(List<Student> aStudent) {

JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token

Obviously Jackson can not deserialize the passed JSON into an Integer. If you insist to send a JSON representation of a User through the request body, you should encapsulate the userId in another bean like the following:

public class User {
private Integer userId;
// getters and setters
}

Then use that bean as your handler method argument:

@RequestMapping(...)
public @ResponseBody Record getRecord(@RequestBody User user) { ... }

If you don't like the overhead of creating another bean, you could pass the userId as part of Path Variable, e.g. /getuser/15. In order to do that:

@RequestMapping(value = "/getuser/{userId}", method = POST, produces = "application/json")
public @ResponseBody Record getRecord(@PathVariable Integer userId) { ... }

Since you no longer send a JSON in the request body, you should remove that consumes attribute.



Related Topics



Leave a reply



Submit