首页 > 解决方案 > 删除使用 GSON 创建的 JSON 字符串中的双引号

问题描述

使用 GSON 库后,我得到了以下火花结果。

[
  "{"A":"1","A-Description":"Eastern                                           "}",
  "{"B":"2","B-Description":"Western                                           "}",
  "{"C":"3","C-Description":"Northern                                          "}",
  "{"D":"4","D-Description":"Southern"}"
]

我想从 json 字符串的开头和结尾删除双引号

最终结果如下:

[
  {"A":"1","A-Description":"Eastern                                           "},
  {"B":"2","B-Description":"Western                                           "},
  {"C":"3","C-Description":"Northern                                          "},
  {"D":"4","D-Description":"Southern"}
]

我已经解决了以下问题:

val jsonString = str.replaceAll("\\\\", "").replaceAll("\"(.+)\"", "$1")

其中 str 是一些字符串。

如果可用,请提出更有效的方法。

标签: javaapache-spark

解决方案


推荐阅读