请建议最好的解决方案来解析多个python字典中的字符串

时间:2017-02-23 10:36:09

标签: python json regex parsing

字符串是:

str = '{"Speed":0,"Tracktime":"2017-02-22 10:14:19.0","Latitude":12.9055766,"gpsstatus":1,"Longitude":77.4990283}{"Speed":1,"Tracktime":"2017-02-23 13:14:19.0","Latitude":12.9355766,"gpsstatus":0,"Longitude":77.5990283}'

我的解决方案:

str.replace("}{","}|{").split("|")

这是最好的方式还是可以更优化的方式完成?

1 个答案:

答案 0 :(得分:1)

试试这个 -

import json
x='{"Speed":0,"Tracktime":"2017-02-22 10:14:19.0","Latitude":12.9055766,"gpsstatus":1,"Longitude":77.4990283}{"Speed":1,"Tracktime":"2017-02-23 13:14:19.0","Latitude":12.9355766,"gpsstatus":0,"Longitude":77.5990283}'
str1=x.split("}{")[0]
str2=x.split("}{")[1]
js1=json.loads(str1+"}")
js2=json.loads("{"+str2)

这不是一个很好的解决方案,但它''为这种字符串工作,你可以使用for循环来创建超过2个obj,我会在我想出一些好的东西后更新它