2017-05-12 39 views
1

我的設備將發送JSON數據是這樣的:如何解析python中的多個json數據?

[{"channel":924125000, "sf":10, "time":"2017-05-11T16:56:15", "gwip":"192.168.1.125", "gwid":"00004c4978dbf5b4", "repeater":"00000000ffffffff", "systype":5, "rssi":-108.0, "snr":17.0, "snr_max":23.3, "snr_min":10.8, "macAddr":"00000000000000c3", "data":"4702483016331210179183", "frameCnt":1, "fport":2}] 

但有時我接收到多個JSON數據(兩個或更多):

[{"channel":924125000, "sf":10, "time":"2017-05-11T16:56:15", "gwip":"192.168.1.125", "gwid":"00001c497b48dbf5", "repeater":"00000000ffffffff", "systype":5, "rssi":-108.0, "snr":17.0, "snr_max":23.3, "snr_min":10.8, "macAddr":"00000000050100e8", "data":"4702483016331210179183", "frameCnt":1, "fport":2}],[{"channel":924125000, "sf":10, "time":"2017-05-11T16:56:15", "gwip":"192.168.1.125", "gwid":"00001c497b48dbf5", "repeater":"00000000ffffffff", "systype":5, "rssi":-108.0, "snr":17.0, "snr_max":23.3, "snr_min":10.8, "macAddr":"00000000050100e8", "data":"4702483016331210179183", "frameCnt":1, "fport":2}] 

當我解析多個JSON數據

json_Dict = json.loads(jsonData) 

then

File "/usr/lib/python2.7/json/decoder.py", line 369, in decode raise ValueError(errmsg("Extra data", s, end, len(s))) ValueError: Extra data: line 1 column 303 - line 1 column 1818 (char 302 - 1817)

如何解析每個多個json數據?

感謝您的幫助

回答

1

,因爲你必須在你的JSON多個對象,你應該包括他們的列表:

json_List = json.loads('[' + jsonData + ']') 
0

在工具粘貼它像JSONLINT

,你會得到:

Error: Parse error on line 17: ...": 1, "fport": 2}], [{ "channel": 924 ---------------------^ Expecting 'EOF', got ','

這是您的錯誤原因。這不是有效的 JSON。 正確的結構可能類似[[...],[...]]。你有[...],[...],這是不正確的。