简体   繁体   English

如何避免 GSON JsonObject 中的反斜杠?

[英]How to avoid backslashes in GSON JsonObject?

I have a Java POJO我有一个 Java POJO

public class TagBean {
  private String type;
  private String id;

  public TagBean(String type, String id) {
    this.type = type;
    this.id = id;       
  }
    // getters
    // setters   
}

I'm building pojo's and adding them to a List, as我正在构建 pojo 并将它们添加到列表中,如

....
List<TagBean> channelsList = new ArrayList<>();
List<TagBean> showsList = new ArrayList<>();
for each <business logic> {
   if value=channels {
      channelsList.add(new TagBean(...));
   }
   if value=shows {
      showsList.add(new TagBean(...));
   }
}

Gson gson = new GsonBuilder().create();
JsonObject tjsonObject = new JsonObject();
tjsonObject.addProperty("channels", gson.toJson(channelsList));
tjsonObject.addProperty("shows", gson.toJson(showsList));

JsonObject mainjsonObject = mainjsonObject.add("tags", tjsonObject);

return mainjsonObject;

My output is:我的输出是:

{
"tags": {
    "channels": "[{\"type\":\"channel\",\"id\":\"channel\",\"name\":\"Channel\",\"parent\":\"SXM\"}]",
    "shows": "[{\"type\":\"shows\",\"id\":\"shows\",\"name\":\"Shows\",\"parent\":\"SXM\"},{\"type\":\"shows\",\"id\":\"howard\",\"name\":\"Howard Stern\",\"parent\":\"shows\"},{\"type\":\"shows\",\"id\":\"howardstern\",\"name\":\"Howard Stern\",\"parent\":\"howard\"}]",
    "sports": "[]"
}
}

How can i remove the backslashes?如何删除反斜杠? So the output is like:所以输出是这样的:

{
  "tags": {
     "channels": " [{"type":"channel","id":"channel","name":"Channel","parent":"SXM"}]",
    "shows": "[{"type":"shows","id":"shows","name":"Shows","parent":"SXM"},{"type":"shows","id":"howard","name":"Howard Stern","parent":"shows"}....

There were few other posts, but none explained this.其他帖子很少,但没有人解释这一点。

The problem is caused by this:问题是由此引起的:

tjsonObject.addProperty("channels", gson.toJson(channelsList));

What that is doing is converting channelsList to a string containing a representation of the list in JSON, then setting the property to that string.所做的是将channelsList转换为包含 JSON 中列表表示形式的字符串,然后将属性设置为该字符串。 Since the string contains JSON meta-characters, they must be escaped when the strings are serialized ... a second time.由于字符串包含 JSON 元字符,因此必须在对字符串进行序列化时对它们进行转义……第二次。

I think that you need to do this instead:我认为你需要这样做:

tjsonObject.add("channels", gson.toJsonTree(channelsList));

That should produce this:那应该产生这个:

{
  "tags": {
     "channels":     
        [{"type":"channel","id":"channel","name":"Channel","parent":"SXM"}],
     "shows": 
        [{"type":"shows","id":"shows","name":"Shows","parent":"SXM"},
         {"type":"shows","id":"howard","name":"Howard Stern","parent":"shows"}
   ....

That is slightly different to what your question asked for, but it has the advantage of being syntactically valid JSON!这与您的问题所要求的略有不同,但它具有语法上有效的 JSON 的优势!

    String mainJsonStr = mainjsonObject.toString();
    mainJsonStr = mainJsonStr.replace("\\\\", ""); //replace the \
    System.out.println(mainJsonStr);

The problem is that gson.toJson returns a String, and问题是gson.toJson返回一个字符串,并且

tjsonObject.addProperty("channels", gson.toJson(channelsList));

this will add channels as a string and not as a JSON object.这会将通道添加为字符串而不是 JSON 对象。 One possible solution is to convert the string returned from gson.toJson to JSON object first then add it to the parent JSON object like一种可能的解决方案是先将从gson.toJson返回的字符串转换为 JSON 对象,然后将其添加到父 JSON 对象中,例如

Gson gson = new GsonBuilder().create();
JsonObject tjsonObject = new JsonObject();
tjsonObject.put("channels", new JsonObject(gson.toJson(channelsList)));
tjsonObject.put("shows", new JsonObject(gson.toJson(showsList)));

this will treat channels and shows as JSON object这会将频道和节目视为 JSON 对象

All strings in java have to escape quotes in them. java中的所有字符串都必须转义其中的引号。 So jsonInString should have slashes in it.所以 jsonInString 应该有斜线。 When you output jsonInString though it shouldn't have the quotes.当你输出 jsonInString 虽然它不应该有引号。 Are you looking at it in a debugger or something?您是在调试器中查看它还是什么?

Just parse json directly and check - will get the output只需直接解析 json 并检查-将得到输出

above solution is not working anymore since GSON 2.8.* use gson.toJsonTree(jsonText).getAsString();自 GSON 2.8.* 使用 gson.toJsonTree(jsonText).getAsString(); 以来,上述解决方案不再有效instead反而

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

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