我有一个存储在 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,它就起作用了。那为什么不呢?