Tes*_*198 0 javascript arrays string parsing angularjs
我有一个像下面这样的字符串,
var x = "[{"k":"1"}]";
console.log(x[0].K);
Run Code Online (Sandbox Code Playgroud)
我把这个字符串挂起来,因为我是从服务器上得到的。它超出了我的控制。我想显示“k”值,当我穿上它时,得到
Uncaught SyntaxError: Unexpected identifier
Run Code Online (Sandbox Code Playgroud)
我知道,这是因为数组内外的“双引号”。
它甚至不允许我解析它。
所以我认为
1)Replace "[ and ]" as '[ and ]' so it becomes
'[{"k":"1"}]'
2)Then I can parse and get the 'k' value.
var x = "[{"k":"1"}]";
console.log(x.replace(/"[/g," '[ ").replace(/"]"/g," ]'"));
Run Code Online (Sandbox Code Playgroud)
但是我仍然遇到相同的意外标识符错误,请任何人建议我帮助。Thnaks。
您仍然在x.周围使用双引号。
var x = '[{"k":"1"}]';
Run Code Online (Sandbox Code Playgroud)
用JSON.parse把它变成一个数组:
var array = JSON.parse(x);
console.log(array); // [{ "k": "1" }]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8411 次 |
| 最近记录: |