为什么 Gson 不使用嵌套转义的 JSON 字符串解析我的文档?



我在GSON上遇到了一个烦人的问题。

我无法解析此文档:

[{"id":0,"assetId":2414775,"shipId":717,"assetType":"0","document":"{"ratios":[{"points":{"x1":0,"y1":673,"x2":3744,"y2":4408},"crop":[{"name":"1_1","width":3744,"height":3735,"path":"1371046546001-RCI-Test-Seas-1306121019_1_1.jpg"},{"name":"x-supersquare","width":1024,"height":1024,"path":"1371046546001-RCI-Test-Seas-1306121019_x-supersquare.jpg"},{"name":"x-square","width":98,"height":98,"path":"1371046546001-RCI-Test-Seas-1306121019_x-square.jpg"},{"name":"x48","width":48,"height":48,"path":"1371046546001-RCI-Test-Seas-1306121019_x48.jpg"}],"name":"1_1","basename":null,"cropped":true},{"points":{"x1":0,"y1":842,"x2":3744,"y2":3650},"crop":[{"name":"4_3","width":3744,"height":2808,"path":"1371046546001-RCI-Test-Seas-1306121019_4_3.jpg"},{"name":"x-superlarge","width":2048,"height":1536,"path":"1371046546001-RCI-Test-Seas-1306121019_x-superlarge.jpg"},{"name":"x-extralarge","width":1024,"height":768,"path":"1371046546001-RCI-Test-Seas-1306121019_x-extralarge.jpg"},{"name":"x-large","width":490,"height":368,"path":"1371046546001-RCI-Test-Seas-1306121019_x-large.jpg"},{"name":"x","width":245,"height":184,"path":"1371046546001-RCI-Test-Seas-1306121019_x.jpg"},{"name":"x86","width":115,"height":86,"path":"1371046546001-RCI-Test-Seas-1306121019_x86.jpg"}],"name":"4_3","basename":null,"cropped":true},{"points":{"x1":0,"y1":322,"x2":3744,"y2":5307},"crop":[{"name":"3_4","width":3744,"height":4985,"path":"1371046546001-RCI-Test-Seas-1306121020_3_4.jpg"},{"name":"x-3-4","width":768,"height":1024,"path":"1371046546001-RCI-Test-Seas-1306121020_x-3-4.jpg"}],"name":"3_4","basename":null,"cropped":true},{"points":{"x1":0,"y1":1755,"x2":3744,"y2":3861},"crop":[{"name":"16_9","width":3744,"height":2106,"path":"1371046546001-RCI-Test-Seas-1306121020_16_9.jpg"},{"name":"x-16-9super","width":2048,"height":1162,"path":"1371046546001-RCI-Test-Seas-1306121020_x-16-9super.jpg"},{"name":"x-16-9","width":1280,"height":720,"path":"1371046546001-RCI-Test-Seas-1306121020_x-16-9.jpg"}],"name":"16_9","basename":null,"cropped":true},{"points":{"x1":295,"y1":0,"x2":3448,"y2":5616},"crop":[{"name":"9_16","width":3153,"height":5616,"path":"1371046546001-RCI-Test-Seas-1306121020_9_16.jpg"},{"name":"x-9-16","width":720,"height":1280,"path":"1371046546001-RCI-Test-Seas-1306121020_x-9-16.jpg"}],"name":"9_16","basename":null,"cropped":true},{"points":{"x1":0,"y1":1221,"x2":3744,"y2":4408},"crop":[{"name":"front_thumb","width":3744,"height":3187,"path":"1371046546001-RCI-Test-Seas-1306121021_front_thumb.jpg"},{"name":"x-front-thumb","width":115,"height":98,"path":"1371046546001-RCI-Test-Seas-1306121021_x-front-thumb.jpg"}],"name":"front_thumb","basename":null,"cropped":true}],"attributes":[{"name":"oImageHeight","value":"5616"},{"name":"oImageWidth","value":"3744"},{"name":"sImageHeight","value":"400"},{"name":"sImageWidth","value":"266"},{"name":"imageCropStatus","value":"1_1:manual;  4_3:manual;  3_4:manual;  16_9:manual;  9_16:manual;  front_thumb:manual;  "},{"name":"credit","value":"Michel Verdure"},{"name":"alternate","value":"Image of Royal Caribbean Allure of the Sease"},{"name":"title","value":"Allure of the Seas"},{"name":"caption","value":"Allure of the Seas - At sea,  by the coast line of Miami\nAllure of the Seas - Royal Caribbean International"},{"name":"datephototaken","value":"11-24-2010"},{"name":"cutline","value":"Allure of the Seas - At sea,  by the coast line of Miami\nAllure of the Seas - Royal Caribbean International"},{"name":"orientation","value":"horizontal"},{"name":"OrigImageName","value":"RCI Allure of the Seas.jpg"},{"name":"imgIndex","value":"1371046546001"},{"name":"imgUniqueName","value":"1371046546001-RCI-Test-Seas.jpg"},{"name":"isImageVisitedByUser","value":"True"},{"name":"isAutoCroppedSetOn","value":"True"},{"name":"publishurl","value":"http://www.gannett-cdn.com/media"},{"name":"rootpath","value":"/TEST/GenericImages/2013/06/12/"},{"name":"basename","value":"1371046546001-RCI-Test-Seas.jpg"},{"name":"smallbasename","value":"1371046546001-RCI-Test-Seas_small.jpg"}],"contributors":[],"id":2414775,"propertyID":1,"siteid":1,"sitename":"TEST","assetGroupId":1,"assetGroupName":"TEST","type":"image","typeid":1,"typeidSpecified":true,"position":0,"positionSpecified":true,"priority":null,"handling":null,"status":"published","dates":{"embargodate":"0001-01-01T00:00:00-05:00","feeddateline":"0001-01-01T00:00:00-05:00"},"TaxonomyEntities":[],"assetdocument":null,"lastaction":null,"CreateDate":"2013-06-12T10:19:15.607-04:00","UpdateDate":"2013-06-12T10:19:39-04:00","CreateUser":"nrifken","UpdateUser":"nrifken","LastPublishedDate":"2013-06-12T10:19:15.607-04:00"}"}]

您可以使用在线工具更好地查看它(即 http://json.parser.online.fr/(

如您所见,"文档"是一个嵌套的 JSON 作为字符串,不知何故,GSON 试图在我认为不应该解析时解析它,我尝试将我的可序列化成员设置为字符串、对象、地图、地图<对象、对象>等,但它们似乎都不起作用。还尝试将其设置为瞬态或删除,以查看 GSON 是否不会打扰解析,但我仍然得到:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 72

我想我对反序列化没有太多控制权,因为我使用的是 RestTemplate 的 GsonHttpMessageConverter。 所以我认为这主要是 e GSON 问题而不是 Springs。

我的模型:

public class ShipAsset {
public ShipAsset(String pID, Integer pAssetID,
        Integer pShipID, String pDocument, String pAssetType) {
    super();
    mID = pID;
    mAssetID = pAssetID;
    mShipID = pShipID;
    mDocument = pDocument;
    mAssetType = pAssetType;
}
public ShipAsset() {
}
@SerializedName("id")
private String mID;
@SerializedName("assetId")
private Integer mAssetID;
@SerializedName("shipId")
private Integer mShipID;
@SerializedName("document")
private String mDocument;
@SerializedName("assetType")
private String mAssetType;
/* getters and setters */ 
}

这就是我所说的:

ShipAsset[] oResults = mGson.fromJson(oJSONString, ShipAsset[].class);

有人有什么想法吗?

谢谢

错误必须出在以下三件事之一中:

1(你正在收到一个JSONArray,你正在这样投射,不是吗?

public class RestFactory {
public static ShipAsset[] getShipsAssets(RestClient restClient) throws Exception {
    ResponseEntity<ShipAsset[]> response = restClient.getForEntity(
            "http://server/rest/yourmethod", ShipAsset[].class);
    return response.getBody();
}
}

2(我不确定是否是这样,但是您没有包含所有字段的构造函数,也没有空的构造器。也许是因为不是完整的pojo,Gson的选角不正确。

3(尝试注释字段idassetType。我很确定,如果您不使用SerializedName注释注释字段,Gson 默认情况下会忽略它。但无论如何,试一试!

祝你好运!

最新更新