无法将JSON反序列化为POJO

时间:2012-04-18 19:49:38

标签: java json gson

我有一个存储在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上并且它有效。那为什么不这样呢?

1 个答案:

答案 0 :(得分:3)

如果您已在数据库字段中使用JSON,请不要再次序列化它。

你需要的只是:

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