java - 如何从字符串中删除第一个“

标签 java json

我从服务器获取此字符串,我需要删除第一个和最后一个“

得到这个

{"TestServiceResult":"[{\"screen_refresh_interval\":4,\"station_list_last_update\":\"update4\"},{\"screen_refresh_interval\":4,\"station_list_last_update\":\"update4\"},{\"screen_refresh_interval\":4,\"station_list_last_update\":\"update4\"},{\"screen_refresh_interval\":4,\"station_list_last_update\":\"update4\"},{\"screen_refresh_interval\":4,\"station_list_last_update\":\"update4\"}]"}

输出

{"TestServiceResult":[{\"screen_refresh_interval\":4,\"station_list_last_update\":\"update4\"},{\"screen_refresh_interval\":4,\"station_list_last_update\":\"update4\"},{\"screen_refresh_interval\":4,\"station_list_last_update\":\"update4\"},{\"screen_refresh_interval\":4,\"station_list_last_update\":\"update4\"},{\"screen_refresh_interval\":4,\"station_list_last_update\":\"update4\"}]}

int index =test.indexOf('[');           
int index2 =test.indexOf(']');

最佳答案

I need to remove first and last "

不,你不知道。您拥有一个 JSON 对象,其中包含一个名为 TestServiceResult 的属性,其值是一个字符串,而该字符串本身恰好是有效的 JSON。您实际需要做的是提取该字符串并将其传递回 JSON 解析器,以便获得一个数组,然后创建一个新的 JSON 对象,其属性 TestServiceResult 的值为该数组,并序列化这个新对象返回到另一个字符串。

仅仅去掉第一个和最后一个引号是不够的,因为这将使其他引号仍然转义为 \" ,这是无效的。

关于java - 如何从字符串中删除第一个“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20471626/

相关文章:

c# - JSON 中的 23,908 个对象 我尝试在将属性添加到 SQL 数据库之前将它们添加到属性中

javascript - 无法使用 THREE.js 导入 Blender 2.79 JSON

java - 应为 BEGIN_OBJECT,但在 Gson 中为 STRING

javascript - 订阅页面加载时的值

java - 如何制作由 map 支持的集合?

javascript - 如何在 java websocket 服务器上读取通过 websocket 发送的 javascript BLOB 数据

java - 使用 HTML/JavaScript 在网页中显示小程序

java - 如何在java中找到两个整数数组之间的相关性

java - Jmagick错误: magick. MagickException:没有可缩放的图像

mysql - 使用 MySQL JSON 字段连接表