我有以下列表需要转换为有效的JSON对象:
data = ['{"id":"0","jsonrpc":"2.0","method":"RoutingRequest","params":{"barcode":"5694501","itemID":113},"timestamp":"2018-08-06T15:38:40.531"}', '']
我尝试过:
import json
my_json = data.decode('utf8').replace("'", '"')
my_json = json.loads(my_json)
继续获取此错误:引发TypeError(f'JSON对象必须 是str,bytes或bytearray,'TypeError:JSON对象必须是str, 个字节或字节数组,而不是列表
我在做什么错? (顺便说一句,我是Python的新手)
答案 0 :(得分:0)
您有一个数据列表。对其进行迭代,然后使用json.load
例如:
import json
data = ['{"id":"0","jsonrpc":"2.0","method":"RoutingRequest","params":{"barcode":"5694501","itemID":113},"timestamp":"2018-08-06T15:38:40.531"}', '']
data = [json.loads(i) for i in data if i] #Iterate your list check if you have data then use json.loads
print(data)
输出:
[{u'params': {u'itemID': 113, u'barcode': u'5694501'}, u'jsonrpc': u'2.0', u'id': u'0', u'timestamp': u'2018-08-06T15:38:40.531', u'method': u'RoutingRequest'}]