gpt4 book ai didi

python - 在python中解析json字符串列表

转载 作者:行者123 更新时间:2023-12-01 01:15:20 26 4
gpt4 key购买 nike

使用传入的字符串,例如列表:

[{"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 elem

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?

<小时/>

我尝试过的:

选项 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)))

这是正确的方法吗?

最佳答案

考虑使用 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}

关于python - 在python中解析json字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54405138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com