1

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 はそれを解析しようとしているのに、そうすべきではないと思うときに、Serializable メンバーを String、Object、Map、Map<Object、Object> として設定しようとしました。などがありますが、どれも機能していないようです。また、GSONが解析を気にしないかどうかを確認するために一時的に設定するか削除しようとしましたが、それでも次のようになります:

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

RestTemplate の GsonHttpMessageConverter を使用しているため、逆シリアル化をあまり制御できないと思います。ですから、Springs よりも主に GSON の問題だと思います。

私のモデル:

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

誰にもアイデアはありますか?

ありがとう

4

1 に答える 1

1

エラーは、次の 3 つのいずれかにある必要があります。

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) フィールドidとに注釈を付けてみてくださいassetType。フィールドに注釈を付けない場合SerializedName、Gsonはデフォルトでそれを無視すると確信しています。とにかく、試してみてください!

幸運を!

于 2013-10-09T03:57:44.637 に答える