2013-10-09 42 views
1

我有一個惱人的問題與GSON。爲什麼Gson沒有使用嵌套轉義的JSON字符串解析我的文檔?

我不能得到這個文件解析:

[{"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); 

人有什麼想法?

感謝

+0

你好!你能向我們展示你的模型類嗎? (你試圖用Gson施展的那個) – 4gus71n

+0

你走了!感謝您抽出時間 –

回答

1

錯誤必須在這三種情況之一:

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。我很肯定,如果你不註釋一個帶有註釋的字段,Gson默認會忽略它。但無論如何,試試吧!

祝你好運!

+0

這沒有奏效,我嘗試過使用GSON(沒有RestTemplate),但它也失敗了......我更新了代碼 –

+0

那麼鑑於錯誤出現在'document'字段開始的列中,爲什麼你不試試讓一個類來投它,而不是使用一個字符串? – 4gus71n

+0

謝謝!搞清楚了,問題在於json文檔沒有在我的字符串中正確掃描......只能添加一個額外的斜線「\」 –