在python中解析json字符串列表

时间:2019-01-28 15:25:05

标签: python

使用列表等传入字符串:

[{"FECHA":"2019-01-28 13:15:42","SERIAL":2,"LONGITUD":-4.2958984375,"LATITUD":50.4469470596,"ID":1,"VALOR":193,"JOURNEYID":1},
{"FECHA":"2019-01-28 13:15:44","SERIAL":3,"LONGITUD":-4.2958984375,"LATITUD":40.4469470596,"ID":1,"VALOR":34,"JOURNEYID":1},
{"FECHA":"2019-01-28 13:15:46","SERIAL":6,"LONGITUD":-3.2958984375,"LATITUD":50.4469470596,"ID":1,"VALOR":14,"JOURNEYID":1},<..>]

长度为N且每个元素的格式如下:

{"FECHA":"2019-01-28 13:15:42","SERIAL":2,"LONGITUD":-4.2958984375,"LATITUD":50.4469470596,"ID":1,"VALOR":193,"JOURNEYID":1}

我想要什么?

迭代列表并分别处理每个JSON元素

first output: {"FECHA":"2019-01-28 13:15:42","SERIAL":2,"LONGITUD":-4.2958984375,"LATITUD":50.4469470596,"ID":1,"VALOR":193,"JOURNEYID":1}

second output: {"FECHA":"2019-01-28 13:15:44","SERIAL":3,"LONGITUD":-4.2958984375,"LATITUD":40.4469470596,"ID":1,"VALOR":34,"JOURNEYID":1}

<...>
  

如何拆分以获取每个JSON元素?


我尝试过的事情:

选项1:

def ParseIncomingDataAzure(message):
    print ("incoming data: {}".format(message))
    x = ast.literal_eval(message)
    for frame in x:
        print("x: {}".format(frame))

输出:

x: {'LATITUD': 50.4469470596, 'FECHA': '2019-01-28 13:15:42', 'JOURNEYID': 1, 'VALOR': 193, 'SERIAL': 2, 'ID': 1, 'LONGITUD': -4.2958984375}
x: {'LATITUD': 40.4469470596, 'FECHA': '2019-01-28 13:15:44', 'JOURNEYID': 1, 'VALOR': 34, 'SERIAL': 3, 'ID': 1, 'LONGITUD': -4.2958984375}
x: {'LATITUD': 50.4469470596, 'FECHA': '2019-01-28 13:15:46', 'JOURNEYID': 1, 'VALOR': 14, 'SERIAL': 6, 'ID': 1, 'LONGITUD': -3.2958984375}

选项2:

def ParseIncomingDataAzure(message):
    messages = message.split(",")
    for frame in messages:
        print("x: {}".format(frame))

输出:

x: [{"FECHA":"2019-01-28 13:15:42"
x: "SERIAL":2
x: "LONGITUD":-4.2958984375
x: "LATITUD":50.4469470596
x: "ID":1
x: "VALOR":193
x: "JOURNEYID":1}
x: {"FECHA":"2019-01-28 13:15:44"
x: "SERIAL":3
x: "LONGITUD":-4.2958984375
x: "LATITUD":40.4469470596
x: "ID":1
x: "VALOR":34
x: "JOURNEYID":1}
x: {"FECHA":"2019-01-28 13:15:46"
x: "SERIAL":6
x: "LONGITUD":-3.2958984375
x: "LATITUD":50.4469470596
x: "ID":1
x: "VALOR":14
x: "JOURNEYID":1}]

选项3:

简单地遍历列表

def ParseIncomingDataAzure(message):
    for frame in message:
         print("x: {}".format(frame))

输出:

x: [
x: {
x: "
x: F
x: E
x: C
x: H
x: A
<....>

可能的解决方案:

def ParseIncomingDataAzure(message):
    print ("incoming data: {}".format(message))
    x = ast.literal_eval(message)
    for frame in x:
        print("x: {}".format(json.dumps(frame)))

这是正确的方法吗?

2 个答案:

答案 0 :(得分:2)

为此考虑使用json包:

In [2]: import json                                                                                                                                                                                                                                                                       
In [3]: s = '{"FECHA":"2019-01-28 13:15:42","SERIAL":2,"LONGITUD":-4.2958984375,"LATITUD":50.4469470596,"ID":1,"VALOR":193,"JOURNEYID":1}'                                                                                                                                                
In [4]: json.loads(s)                                                                                                                                                                                                                                                                     
Out[4]: 
{'FECHA': '2019-01-28 13:15:42',
 'SERIAL': 2,
 'LONGITUD': -4.2958984375,
 'LATITUD': 50.4469470596,
 'ID': 1,
 'VALOR': 193,
 'JOURNEYID': 1}

答案 1 :(得分:0)

这两个功能解决了我的用例:

def ParseIncomingDataAzure(message):
    print ("incoming data: {}".format(message))
    x = ast.literal_eval(message)
    for frame in x:
        print("x: {}".format(json.dumps(frame)))
        <...>

以下功能的不便之处在于,您将需要知道“,”分隔传入列表的每个元素的位置,因此对于真实数据,其中每个元素中json的字段数可能会不同不能使用的列表:

def ParseIncomingDataAzure(message):
    n = 7
    frames = message.split(",")
    while frames:
        y= ','.join(frames[:n])
        frames = frames[7:]
        print (y)