简体   繁体   English

在python中解析json字符串列表

[英]Parse list of json string in python

Using an incoming string like list: 使用列表等传入字符串:

[{"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},<..>]

With a lenght N and each element with the following format: 长度为N且每个元素具有以下格式:

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

What I want? 我想要的是?

Iterate the list and process each JSON elem individually 迭代列表并分别处理每个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}

<...>

How could I split to take each JSON element? 如何拆分以获取每个JSON元素?


What I have tried: 我尝试过的

Option 1: 选项1:

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

Output: 输出:

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}

Option 2: 选项2:

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

Output: 输出:

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}]

Option 3: 选项3:

Simple for to iterate over the list 简单遍历列表

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

Output: 输出:

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

Possible solution: 可能的解决方案:

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

Is this the correct way? 这是正确的方法吗?

Consider using the json package for this: 考虑为此使用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}

This both functions solved my use case: 这两个功能解决了我的用例:

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

The inconvenient of the following function is that you will need to know the position where the "," is separating each element of the incoming list, so for real data where the number of fields of the json could be different in each element of the list you can not use: 以下功能的不便之处在于,您将需要知道“,”分隔传入列表的每个元素的位置,因此对于真实数据,其中列表的每个元素中json的字段数可能会不同您不能使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM