Java 为什么不是';GST使用嵌套的转义JSON字符串解析我的文档?

Java 为什么不是';GST使用嵌套的转义JSON字符串解析我的文档?,java,json,gson,resttemplate,Java,Json,Gson,Resttemplate,我和格森之间有个恼人的问题 我无法解析此文档: [{"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

我和格森之间有个恼人的问题

我无法解析此文档:

[{"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\"}"}]
您可以使用在线工具更好地查看它(即)

正如您所看到的,“文档”是一个嵌套的JSON字符串,GSON试图解析它,但我认为不应该,我尝试将我的可序列化成员设置为字符串、对象、映射、映射等,但它们似乎都不起作用。还尝试将其设置为transient或removing,以查看GSON是否会费心解析,但我仍然得到:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 72
我想我对反序列化没有太多的控制,因为我使用的是RestTemplate的GsonHttpMessageConverter。因此,我认为这主要是Egson的问题,而不是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();
}

}
公共类RestFactory{
公共静态ShipAsset[]getShipsAssets(RestClient RestClient)引发异常{
ResponseEntity response=restClient.getForEntity(
"http://server/rest/yourmethod“,船舶资产[]类别);
返回response.getBody();
}
}
2) 我不确定这是不是因为这个,但是你没有一个包含所有字段的构造函数,也没有一个空的构造函数。也许是因为不是一个完整的pojo,Gson没有正确地选角

3) 尝试对字段
id
assetType
进行注释。我很确定,如果不使用
SerializedName
注释对字段进行注释,默认情况下Gson会忽略它。但无论如何,试试看


祝你好运

错误必须在以下三个方面之一:

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

}
公共类RestFactory{
公共静态ShipAsset[]getShipsAssets(RestClient RestClient)引发异常{
ResponseEntity response=restClient.getForEntity(
"http://server/rest/yourmethod“,船舶资产[]类别);
返回response.getBody();
}
}
2) 我不确定这是不是因为这个,但是你没有一个包含所有字段的构造函数,也没有一个空的构造函数。也许是因为不是一个完整的pojo,Gson没有正确地选角

3) 尝试对字段
id
assetType
进行注释。我很确定,如果不使用
SerializedName
注释对字段进行注释,默认情况下Gson会忽略它。但无论如何,试试看


祝你好运

你好!你能给我们看看你的模型课吗?(你正试着和格森一起投的那个)好了!谢谢你抽出时间嗨!你能给我们看看你的模型课吗?(你正试着和格森一起投的那个)好了!感谢您抽出时间,但没有成功,我尝试使用GSON(没有RestTemplate),但也失败了。。。我更新了代码井,因为错误出现在
文档
字段开始的列中,为什么不尝试创建一个类来强制转换它,而不是使用字符串?谢谢!我发现,问题是json文档没有在我的字符串中正确地替换。。。只是添加了一个额外的斜杠“\”不起作用,我尝试使用GSON(没有RestTemplate),但也失败了。。。我更新了代码井,因为错误出现在
文档
字段开始的列中,为什么不尝试创建一个类来强制转换它,而不是使用字符串?谢谢!我发现,问题是json文档没有在我的字符串中正确地替换。。。只需添加一个额外的斜杠“\”