gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:21:23 25 4
gpt4 key购买 nike

我在使用 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 试图解析它,当我认为它不应该时,我尝试将我的可序列化成员设置为 String、Object、Map、Map 等,但它们似乎都不起作用。还尝试将其设置为 transient 或删除以查看 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 默认情况下会忽略它。但无论如何,请尝试一下!

祝你好运!

关于java - 为什么 Gson 不使用嵌套转义 JSON 字符串解析我的文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19260836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com