簡體   English   中英

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

[英]How to avoid backslashes in GSON JsonObject?

我有一個 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   
}

我正在構建 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;

我的輸出是:

{
"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": "[]"
}
}

如何刪除反斜杠? 所以輸出是這樣的:

{
  "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"}....

其他帖子很少,但沒有人解釋這一點。

問題是由此引起的:

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

所做的是將channelsList轉換為包含 JSON 中列表表示形式的字符串,然后將屬性設置為該字符串。 由於字符串包含 JSON 元字符,因此必須在對字符串進行序列化時對它們進行轉義……第二次。

我認為你需要這樣做:

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

那應該產生這個:

{
  "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"}
   ....

這與您的問題所要求的略有不同,但它具有語法上有效的 JSON 的優勢!

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

問題是gson.toJson返回一個字符串,並且

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

這會將通道添加為字符串而不是 JSON 對象。 一種可能的解決方案是先將從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)));

這會將頻道和節目視為 JSON 對象

java中的所有字符串都必須轉義其中的引號。 所以 jsonInString 應該有斜線。 當你輸出 jsonInString 雖然它不應該有引號。 您是在調試器中查看它還是什么?

只需直接解析 json 並檢查-將得到輸出

自 GSON 2.8.* 使用 gson.toJsonTree(jsonText).getAsString(); 以來,上述解決方案不再有效反而

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM