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):

janusz j
  • 321
  • 3
  • 17
  • Does this answer your question? [How to convert JSON string into List of Java object?](https://stackoverflow.com/questions/44589381/how-to-convert-json-string-into-list-of-java-object) –  Feb 21 '20 at 08: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 – janusz j Feb 21 '20 at 08:42

1 Answers1

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.

Gaetano Piazzolla
  • 1,388
  • 1
  • 16
  • 31