3

I am trying to convert JSON String (has to be the List) to Object List:

[   
    {
        "paramName":"Content-Type",
         "paramValue":"text/xml; charset\u003d\"utf-8\""
    }
]

This is my Service.class:

String jSONString = new Gson().toJson(systemParams);
ObjectMapper mapper = new ObjectMapper();
List<A> map = (List<A>) mapper.readValue(jSONString, A.class);

This is my model:

@Data
@AllArgsConstructor
    public class A {

        String paramName;
        String paramValue;
    }

But I obtained exception:

Cannot deserialize instance of java.util.LinkedHashMap

So I tried in different way:

A[] map = mapper.readValue(jSONString, A[].class);

And I obtained:

InvalidDefinitionException: Cannot construct instance of com.model.A (no Creators, like default construct, exist):

UPDATE:

I tried to change Service.class for:

List<A> map = mapper.readValue(jSONString, new TypeReference<List<A>>(){});

Still got an exception:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of com.model.A (no Creators, like default construct, exist):

So I deleted @AllArgsConstructor annotation and added Constructor manually:

   public A(String paramName, String paramValue) {}

And still an exception:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of com.model.A (no Creators, like default construct, exist):

2
  • Does this answer your question? How to convert JSON string into List of Java object? Commented Feb 21, 2020 at 8:17
  • @wowo No, now I have: InvalidDefinitionException: Cannot construct instance of com.model.A (no Creators, like default construct, exist) but I use @Data and @AllArgsConstructor annotations in A.class Commented Feb 21, 2020 at 8:42

1 Answer 1

1

I think that the problem is written in the error: try to define an empty constructor on the class that have to be deserialized. I think also that to serialize the class ( so, the other way, from byte to class) you have to implement the serializable interface.

Sign up to request clarification or add additional context in comments.

1 Comment

I added empty constructor and it works now. Thank you!

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.