无法反序列化JSON到POJO(Cannot deserialize JSON to POJO)

2019-10-16 15:22发布

我有存储在DB一个JSON,我想它反序列化到一个POJO

JSON in DB

{
 "requests": [
  {
    "url": "http://localhost:8080/wst-server-1.0-SNAPSHOT/services/rest/",
    "path": "3/reset",
    "method": "POST",
    "statusCode": "204",
    "statusMessage": "No Content"
  },
  {
    "url": "http://localhost:8080/wst-server-1.0-SNAPSHOT/services/rest/",
    "path": "3/orders",
    "method": "POST",
    "statusCode": "201",
    "statusMessage": "Created"
  }  
 ]
}

the code

Gson gson = new Gson();
String json = gson.toJson(rs.getString(Columns.REQUEST.getName()));
logger.info(json);
ClientTask task = gson.fromJson(json, ClientTask.class);

logger.info(json)给出以下

"{\n  \"requests\": [\n    {\n      \"url\": \"http://localhost:8080/wst-server-1.0-SNAPSHOT/services/rest/\",\n      \"path\": \"3/reset\",\n      \"method\": \"POST\",\n      \"statusCode\": \"204\",\n      \"statusMessage\": \"No Content\"\n    },\n    {\n      \"url\": \"http://localhost:8080/wst-server-1.0-SNAPSHOT/services/rest/\",\n      \"path\": \"3/orders\",\n      \"method\": \"POST\",\n      \"statusCode\": \"201\",\n      \"statusMessage\": \"Created\"\n    }\n  ]\n}"

gson.fromJson(json, ClientTask.class); 行抛出该异常

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 486
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:180)
at com.google.gson.Gson.fromJson(Gson.java:755)
at com.google.gson.Gson.fromJson(Gson.java:721)
at com.google.gson.Gson.fromJson(Gson.java:670)
at com.google.gson.Gson.fromJson(Gson.java:642)
at cz.cvut.fit.wst.database.rowmapper.ClientTaskRowMapper.mapRow(ClientTaskRowMapper.java:30)
at cz.cvut.fit.wst.database.rowmapper.ClientTaskRowMapper.mapRow(ClientTaskRowMapper.java:1)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:92)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:1)
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:648)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:586)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:636)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:665)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:673)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:728)
at cz.cvut.fit.wst.database.management.MySqlManager.getClientTask(MySqlManager.java:132)
at cz.cvut.fit.wst.client.rest.RestFrontend.main(RestFrontend.java:29)

Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 486
at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:168)
... 16 more

POJOs

public class ClientTask {

    private String taskTypeId;
    private String userId;

    private List<ClientTaskRequest> requests;

}

public class ClientTaskRequest {

    private String url;
    private String path;
    private String method;
    private String body;
    private String statusCode;
    private String statusMessage;
    private String responseParser;

}

我试图手动复制日志记录器给出字符串并投入GSON是正确的,它的工作。 那么,为什么这不?

Answer 1:

如果你已经有了JSON在数据库领域,不要再对其进行序列化。

所有你应该需要的是:

Gson gson = new Gson();
String json = rs.getString(Columns.REQUEST.getName());
ClientTask task = gson.fromJson(json, ClientTask.class);


文章来源: Cannot deserialize JSON to POJO
标签: java json gson