1
我知道這已經是問的問題,但我沒有得到來自先前提出的任何問題的答案...轉換一個無效的JSON對象,以有效的JSON對象在JavaScript
我的問題是 - 服務器發送JSON的數組對象作爲字符串,我想將字符串轉換爲有效的JSON數組。
例如:服務器發送這樣
'{
"ts": "3170075",
"eventid": 25,
"oper_minutes": 48577,
"time": "2016-12-02T06:36:36Z",
"lto": "7200000",
"package_name": "XXX",
"timestamp": "1480660593188",
"action_type": "1",
"events": [{
"device_name": 0,
"key_category": 1,
"eventid": 15,
"ts": "2111"
}, {
"power_state": 3,
"reason": 1,
"eventid": 1,
"ts": "2113"
}]
}, {
"ts": "3170084",
"eventid": 25,
"oper_minutes": 48577,
"time": "2016-12-02T06:36:36Z",
"lto": "7200000",
"package_name": "XXXX",
"timestamp": "1480660593218",
"action_type": "1",
"events": [{
"device_name": 0,
"key_category": 1,
"eventid": 15,
"ts": "2111"
}, {
"power_state": 3,
"reason": 1,
"eventid": 1,
"ts": "2113"
}]
}'
我想將它轉化成使用純JavaScript兩個對象的有效陣列。
實施例:
[{
"ts": "3170075",
"eventid": 25,
"oper_minutes": 48577,
"time": "2016-12-02T06:36:36Z",
"lto": "7200000",
"package_name": "XXX",
"timestamp": "1480660593188",
"action_type": "1",
"events": [{
"device_name": 0,
"key_category": 1,
"eventid": 15,
"ts": "2111"
}, {
"power_state": 3,
"reason": 1,
"eventid": 1,
"ts": "2113"
}]
}, {
"ts": "3170084",
"eventid": 25,
"oper_minutes": 48577,
"time": "2016-12-02T06:36:36Z",
"lto": "7200000",
"package_name": "XXX",
"timestamp": "1480660593218",
"action_type": "1",
"events": [{
"device_name": 0,
"key_category": 1,
"eventid": 15,
"ts": "2111"
}, {
"power_state": 3,
"reason": 1,
"eventid": 1,
"ts": "2113"
}]
}]
我已經嘗試過JSON.parse,JSON.stringify等
如預期沒有有效的結果..
注:對象的這種陣列可以是任何大小,服務器有時會發送1000個對象,有時僅發送10個字符串。不應該花費太多時間來處理。
請幫助,我堅持了這一近一天..我在nodeJS中使用這個只有純javaScript或與NPM庫。