我有一个包含多个列表的字符串,每个列表包含多个json对象
'[{"user": "use1", "fullname": "full1"}][{"user": "use2", "fullname": "full2"}, {"user": "use3", "fullname": "full3"}][{"user": "use4", "fullname": "full4"}]'
预期结果应该是
[{"user": "use1", "fullname": "full1"},{"user": "use2", "fullname": "full2"},{"user": "use3", "fullname": "full3"},{"user": "use4", "fullname": "full4"}]
如何将json对象放入一个列表中?
答案 0 :(得分:0)
data = data.split('[')[1:]
data=[line[:-1] for line in data]
elts = []
for i in range(len(data)):
try:
elt = json.loads(data[i], encoding='utf-8')
elts.append(elt)
except Exception as e:
line= '[' + data[i] + ']'
elt = json.loads(line, encoding='utf-8')
elts.extend(elt)