我从服务器获取此字符串,我需要删除第一个和最后一个“
得到这个
{"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/