Convert String with json to List

janusz j :

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

Gaetano :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=3616&siteId=1