【发布时间】:2019-04-16 02:52:26
【问题描述】:
我有一个这样的输入 Json 字符串:
var x= "[{\"name\":\"ahmed\",\"age\":\"26\"},
{\"name\":\"Sam\",\"age\":\"25\"}]"
我想在不删除分隔符的情况下将其拆分为从 { 到 } 的字符串列表,如下所示
var list;
list[0]= {\"name\":\"ahmed\",\"age\":\"26\"}
list[1]= {\"name\":\"Sam\",\"age\":\"25\"}
使用 split 方法会删除分隔符并且不会产生正确的格式
x= x.replace(/\[\/]/g, '/'); //to remove [ and ]
x= x.replace( /},/ ,'}\n' ); // does not split the string to list of strings
list = x; // type mismatch error since x is a string and list is array
【问题讨论】:
-
使用 JSON.parse 解析 JSON 怎么样?
-
@Teemu 非常感谢.. 不知道我怎么没想到...
标签: javascript string replace split substring