1
我想提供的字符串轉換成對象數組獲得的SyntaxError意外的標記非法而將字符串轉換爲對象
var string = "[{cpu:100,memory:47,freeDisk:83,networks:[{name: \"ctm-4680b9b244c\", status : \"active\"},{name: \"ctm-c2f21106323\", status : \"active\"},{name: \"ctz-1977174d443\", status : \"active\"},{name: \"ctz-1b49d8ccfa5\", status : \"active\"},{name: \"ctz-28c7ceda9ce\", status : \"active\"},{name: \"ctz-3337b7f252a\", status : \"active\"},{name: \"ctz-bbe9792d0c7\", status : \"active\"},{name: \"ctz-d400e32307d\", status : \"active\"},{name: \"ens33\", status : \"active\"},{name: \"lo\", status : \"active\"},{name: \"ctz-9bf91c248f9\", status : \"inactive\"}],infrastructurename:\"ct-colormaster-host\", infrastructurestatus:\"running\", infrastructureuptime: \"Sat Aug 12 00:00:00 UTC 2017\", infrastructureuptimeSince:{ days: 0, hours: 3, minutes: 39},infrastructuredowntime:', infrastructuredowntimeSince:{days: 0, hours: 0, minutes: 0}, infrastructurecurrentCheckTime : \"Sat Aug 12 06:23:25 UTC 2017\"}]"
我需要將其轉換成
[
{
cpu:100,
memory: 47,
freeDisk: 83,
networks: [
{
name: "ctm-4680b9b244c",
status: "active"
},
{
name: "ctm-c2f21106323",
status: "active"
},
...
]
}
]
我試圖JSON.parse
和eval
但他們都沒有工作,並給我意外令牌非法錯誤。
請建議我如何將此字符串轉換爲對象。
爲什麼它是一個字符串?只需用JavaScript對象文字聲明它即可。 – Pointy
我不知道,但從彈性搜索中獲取此字符串 – SpunkyLive
字符串的內容是有效的JavaScript語法,但不是有效的JSON語法。 – Pointy