简体   繁体   English

使用 GSON 将 json 字符串转换为特定类的问题

[英]Problem in converting json string to specific class with GSON

I need to send a class to an API with JSON format and then, I need to deserialize it on the destination function.我需要将一个类发送到 JSON 格式的 API,然后,我需要在目标函数上反序列化它。 So, I'm using GSON on java.所以,我在 Java 上使用 GSON。 First, I tried to serialize it with the following method:首先,我尝试使用以下方法对其进行序列化:

Gson gson = new Gson();
JsonObject userPpfi = new JsonObject();
userPpfi.addProperty("user", "shahab@gmail.com");
userPpfi.addProperty("ppfi", gson.toJson(oppfi));

and after getting it on the destination, I use the following codes to get these parameters.到达目的地后,我使用以下代码获取这些参数。 I can get the user successfully but when I need to deserialize the second method with the GSON, the error will occur.我可以成功获取用户,但是当我需要使用 GSON 反序列化第二种方法时,就会发生错误。

JsonObject jelement = new JsonParser().parse(message).getAsJsonObject();
user = jelement.get("user").
ppfi = jelement.get("ppfi").toString();
System.out.println("new ppfi : " + gson.fromJson(ppfi,PPFI.class).getId());

And the error is:错误是:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $ com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期为 BEGIN_OBJECT 但在第 1 行第 2 列路径 $

So, how can I use the PPFI Class in the destination?那么,如何在目标中使用 PPFI 类? Thanks.谢谢。

sample message示例消息

{"user":"shahab@gmail.com","ppfi":"{\"_id\":\"1213\",\"Name\":\"something\",\"item\":5,\"Items\":[{\"Type\":\"pizza\",\"Count\":0},{\"Type\":\"drink\",\"Count\":0},{\"Type\":\"soup\",\"Count\":0}],\"itemtime\":\"Sep 28, 2020, 9:25:31 AM\",\"itemtime2\":\"Sep 28, 2020, 8:25:31 AM\",\"itemtime3\":\"Sep 28, 2020, 10:55:39 AM\",\"itemspec\":[]}"}

and I know this is the main problem (the " of the beginning of the following line):我知道这是主要问题(以下行开头的“):

"{\"_id\":\"1213\",\"Name\":\"something\",\"item\":5,\"Items\":[{\"Type\":\"pizza\",\"Count\":0},{\"Type\":\"drink\",\"Count\":0},{\"Type\":\"soup\",\"Count\":0}],\"itemtime\":\"Sep 28, 2020, 9:25:31 AM\",\"itemtime2\":\"Sep 28, 2020, 8:25:31 AM\",\"itemtime3\":\"Sep 28, 2020, 10:55:39 AM\",\"itemspec\":[]}"

How can I convert the value of ppfi to the PPFI class??如何将 ppfi 的值转换为 PPFI 类?? (this value is generated by the GSON as I mentioned before) (这个值是由我之前提到的 GSON 生成的)

Couple of quick things,一些快速的事情,

  1. Sharing the json content would help.共享 json 内容会有所帮助。
  2. Based on exception message, expected is JsonObject which begins with "{", but there is a string.根据异常消息,预期是以“{”开头的JsonObject,但有一个字符串。

Again by looking at Json we can confirm what appropriate fix would be.再次通过查看 Json,我们可以确认什么是适当的修复。

Finally, I found out about the problem.最后,我发现了这个问题。 As I used the JsonObject.addProperty("","") , the value was converting to the string and in the destination, I wasn't successful to convert the string to the final instance of the class.当我使用JsonObject.addProperty("","") ,该值正在转换为字符串,并且在目标中,我没有成功将字符串转换为类的最终实例。 So, there are two solutions.所以,有两种解决方案。

Changing the source(preferred)更改来源(首选)

I added the ppfi value as a string using gson.toJson(ppfi) .我使用gson.toJson(ppfi)添加了 ppfi 值作为字符串。 This function will return the json string.此函数将返回 json 字符串。 So, I changed it to a JsonTree and I put the class to the JsonTree .因此,我将其更改为JsonTree并将类放入JsonTree So, this is the latest version of my source function:所以,这是我的源函数的最新版本:

Gson gson = new Gson();
JsonObject userPpfi = new JsonObject();
userPpfi.addProperty("user", "shahab@gmail.com");
userPpfi.add("ppfi", new Gson().toJsonTree(oppfi));
sqs.sendMessage((gson.toJson(userPpfi)));

and in the destination function, I use it as a simple Json String.在目标函数中,我将其用作简单的 Json 字符串。

String ppfiFromMessage = jelement.get("ppfi").toString();
currentPPfi = gson.fromJson(ppfiFromMessage,PPFI.class);

And now, I can use the currentPPFI .现在,我可以使用currentPPFI

Changing only the destination只更改目的地

You can only replace all of the \\ from the PPFI.您只能替换 PPFI 中的所有\\ So, just use所以,只需使用

JsonObject jelement = new JsonParser().parse(message).getAsJsonObject();
user = jelement.get("user").
ppfi = jelement.get("ppfi").toString().replace("\\","");
System.out.println("new ppfi : " + gson.fromJson(ppfi,PPFI.class).getId());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM