使用 python 从列表中提取 json 个对象

Extract json objects from a list using python

我有一个字符串类型的列,其中包含一个元素列表,这些元素是嵌套的字典对象。例如

str = '[{"method":
{"super":0.03,"normal":0.8,"par":0.15,"goal":0.01,"fact":0.04},
"city":["nyc","atlanta"],
"description":"some description",
"content_type":"media"},
{"method":
{"super":0.03,"normal":0.8,"par":0.15,"goal":0.01,"fact":0.04},
"city":["chicago","dallas"],
"description":"some description2",
"content_type":"web"},
{"method":
{"super":0.03,"normal":0.8,"par":0.15,"goal":0.01,"fact":0.04},
"city":["las vegas","buffalo"],
"description":"some description3",
"content_type":"media"}]'

这实际上是spark dataframe中的一个字符串类型的列。所以我想知道如何将字符串的内容转换为列表,以便我可以使用 json.loads.

转换列表中的每个元素

有什么想法吗?

json.loads 应该可以很好地处理这些数据 - 它会 return 一个字典列表。

有几个步骤可以解决这个问题,我希望 json.loads 是您完成后所需要的。 如果您按照这些步骤操作,您的字符串将转换为字典列表(几乎不需要 json.loads)

第一步,去掉字符串中的括号“[]” 假设您将字符串设置为变量 "value" 运行 这些行:

value = value[1:]
value = value[:-1]

接下来,我们将准备使用 python 的 .split() 函数,该函数可以将任何字符串转换为列表,方法是将字符串用任意字符分隔 select。 问题是,您的字符串中有两层逗号。一个在列表之间,一个在字典值之间。 因此,在我们拆分行之前,让我们将列表的逗号变成半色,以便我们的拆分函数可以将字符串吐到正确的位置。使用此代码:

value = value.replace('},','};') 

最后,使用此代码将字符串拆分为列表 值 = value.split(';')

让我知道这是否适合你 祝你好运

import json
msg = '''[{"method":
{"super":0.03,"normal":0.8,"par":0.15,"goal":0.01,"fact":0.04},
"city":["nyc","atlanta"],
"description":"some description",
"content_type":"media"},
{"method":
{"super":0.03,"normal":0.8,"par":0.15,"goal":0.01,"fact":0.04},
"city":["chicago","dallas"],
"description":"some description2",
"content_type":"web"},
{"method":
{"super":0.03,"normal":0.8,"par":0.15,"goal":0.01,"fact":0.04},
"city":["las vegas","buffalo"],
"description":"some description3",
"content_type":"media"}]'''
json.loads(msg)[0]



 out:
{'city': ['nyc', 'atlanta'],
 'content_type': 'media',
 'description': 'some description',
 'method': {'fact': 0.04,
  'goal': 0.01,
  'normal': 0.8,
  'par': 0.15,
  'super': 0.03}}