js json字符串转json
今天js转json字符串为json的时候竟然报错了
VM7011:1 Uncaught SyntaxError: Unexpected number in JSON at position 46 at JSON.parse (<anonymous>) at WebSocket.ws.onmessage (socket_service.js?5b8c:69)
仔细看了下报错的位置,大致知道了原因,json字符串中 1:0, 2:0, 这种key value的形式,key没有包裹双引号,那么解决方案有两种,代码如下:
方案一:使用eval
var data = '{"data":{"dev:2018030994:2c:fb":{"analogMap":{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0},"devicesn":2018030994,"eventTime":1632464989011,"eventTimeStr":"2021-09-24 14:29:49","flag":251,"flagMsg":"O2"},"dev:2018030994:2c:fc":{"analogMap":{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0},"devicesn":2018030994,"eventTime":1632464988011,"eventTimeStr":"2021-09-24 14:29:48","flag":252,"flagMsg":"SF6"},"dev:2018030994:2c:fd":{"analogMap":{1:0.0000,2:0.0000,3:0.0000,4:0.0000,5:0.0000,6:0.0000},"devicesn":2018030994,"eventTime":1632464987040,"eventTimeStr":"2021-09-24 14:29:47","flag":253,"flagMsg":"AD"},"dev:2018030994:2c:fe":{"analogMap":{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0,16:0,17:0,18:0,19:0,20:0,21:0,22:0,23:0,24:0,25:0,26:0,27:0,28:0,29:0,30:0,31:0,32:0},"devicesn":2018030994,"eventTime":1632464992011,"eventTimeStr":"2021-09-24 14:29:52","flag":254,"flagMsg":"电缆温度"},"dev:2018030994:2c:ff":{"analogMap":{1:50,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0,16:0,17:0,18:0,19:0,20:0,21:0,22:0,23:0,24:0,25:0,26:0,27:0,28:0,29:0,30:0,31:0,32:0},"devicesn":2018030994,"eventTime":1632472082395,"eventTimeStr":"2021-09-24 16:28:02","flag":255,"flagMsg":"湿度"},"dev:2018030994:2c:0":{"analogMap":{1:28,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0,16:0,17:0,18:0,19:0,20:0,21:0,22:0,23:0,24:0,25:0,26:0,27:0,28:0,29:0,30:0,31:0,32:0},"devicesn":2018030994,"eventTime":1632469997510,"eventTimeStr":"2021-09-24 15:53:17","flag":0,"flagMsg":"温度"}},"type":"dev_data"}' eval("(" + data + ")")
方案二:给数字的key包裹上双引号,要匹配的有两种情况,1 - {数字:2 - ,数字:,(花括号+数字+冒号)或者(逗号+数字+冒号)
var data = '{"data":{"dev:2018030994:2c:fb":{"analogMap":{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0},"devicesn":2018030994,"eventTime":1632464989011,"eventTimeStr":"2021-09-24 14:29:49","flag":251,"flagMsg":"O2"},"dev:2018030994:2c:fc":{"analogMap":{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0},"devicesn":2018030994,"eventTime":1632464988011,"eventTimeStr":"2021-09-24 14:29:48","flag":252,"flagMsg":"SF6"},"dev:2018030994:2c:fd":{"analogMap":{1:0.0000,2:0.0000,3:0.0000,4:0.0000,5:0.0000,6:0.0000},"devicesn":2018030994,"eventTime":1632464987040,"eventTimeStr":"2021-09-24 14:29:47","flag":253,"flagMsg":"AD"},"dev:2018030994:2c:fe":{"analogMap":{1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0,16:0,17:0,18:0,19:0,20:0,21:0,22:0,23:0,24:0,25:0,26:0,27:0,28:0,29:0,30:0,31:0,32:0},"devicesn":2018030994,"eventTime":1632464992011,"eventTimeStr":"2021-09-24 14:29:52","flag":254,"flagMsg":"电缆温度"},"dev:2018030994:2c:ff":{"analogMap":{1:50,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0,16:0,17:0,18:0,19:0,20:0,21:0,22:0,23:0,24:0,25:0,26:0,27:0,28:0,29:0,30:0,31:0,32:0},"devicesn":2018030994,"eventTime":1632472082395,"eventTimeStr":"2021-09-24 16:28:02","flag":255,"flagMsg":"湿度"},"dev:2018030994:2c:0":{"analogMap":{1:28,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0,16:0,17:0,18:0,19:0,20:0,21:0,22:0,23:0,24:0,25:0,26:0,27:0,28:0,29:0,30:0,31:0,32:0},"devicesn":2018030994,"eventTime":1632469997510,"eventTimeStr":"2021-09-24 15:53:17","flag":0,"flagMsg":"温度"}},"type":"dev_data"}' data = data.replace(/\{(\d+):/g, "\{\"$1\":") data = data.replace(/\,(\d+):/g, "\,\"$1\":") JSON.parse(data)
感觉方案一要简单好多呀