最佳方式:Facebook JSON解析到MongoDB(Python3)
我试图找到解析Facebook对MongoDB的JSON响应的最佳方法 FB事件JSON:最佳方式:Facebook JSON解析到MongoDB(Python3),python,json,mongodb,facebook,Python,Json,Mongodb,Facebook,我试图找到解析Facebook对MongoDB的JSON响应的最佳方法 FB事件JSON: { "description": "Event description", "name": "Event name", "place": { "name": "Place name", "location": { "city": "City", "country": "Country", "latitude": -26.31604,
{
"description": "Event description",
"name": "Event name",
"place": {
"name": "Place name",
"location": {
"city": "City",
"country": "Country",
"latitude": -26.31604,
"longitude": -48.83667,
"state": "State",
"street": "Street address",
"zip": "000000"
},
"id": "429579150543987"
},
"start_time": "2017-10-27T19:00:00-0200",
"id": "1557095844358825"
}
有时,事件没有上面的所有字段,即:location
和place
是可选的
所以我做了:
event_dict = {
'id': event['id'],
'name': event['name'],
'start_time': event['start_time']
}
if ("place" in event):
if ("name" in event['place']):
event_dict['place'] = event['place']['name']
if ("location" in event['place']):
if ("street" in event['place']['location']):
event_dict['street'] = event['place']['location']['street']
if ("zip" in event['place']['location']):
event_dict['zip'] = event['place']['location']['zip']
if ("city" in event['place']['location']):
event_dict['city'] = event['place']['location']['city']
if ("state" in event['place']['location']):
event_dict['state'] = event['place']['location']['state']
if ("country" in event['place']['location']):
event_dict['country'] = event['place']['location']['country']
if ("end_time" in event):
event_dict['end_time'] = event['end_time']
if ("description" in event):
event_dict['description'] = event['description']
event_list.append(event_dict)
event\u list
是我保存在MongoDB上的dict列表
我的主要问题是:有没有更好的方法来代替很多if-then-else条件?如果要将json对象转换为dict,只需执行以下操作:
import json
event_dict = json.loads(event)
要将所有内容转换为一级字典,可以使用递归函数遍历字典,并在出现键值对时提取它们。大概是这样的:
event = {
"description": "Event description",
"name": "Event name",
"place": {
"name": "Place name",
"location": {
"city": "City",
"country": "Country",
"latitude": -26.31604,
"longitude": -48.83667,
"state": "State",
"street": "Street address",
"zip": "000000"
},
"id": "429579150543987"
},
"start_time": "2017-10-27T19:00:00-0200",
"id": "1557095844358825"
}
one_level_dict = {}
def recursive_extraction(dict, base_string = ""):
for key, value in dict.items():
if type(value) is type({}):
recursive_extraction(value, base_string + key + "_")
else:
one_level_dict[base_string + key] = value
recursive_extraction(event)
print(one_level_dict)
这有点零碎,但您应该能够根据自己的目的对其进行修改
(这是针对python3的,对于python2,我认为您需要用dict.iteritems()替换dict.items())
编辑:添加了一个基本字符串,以维护较低级别值的json结构上下文。您可以将大多数if语句替换为
key:event.get(“key”,None)
或嵌套属性val:event.get('key1',None.).get('key2',None)
因此您需要一个一级dict作为输出,该dict中是否有键值对?@chasmani是的,但原始JSON中的某些字段是可选的。我的代码正在运行,但我不认为这是更好的方法。嘿,伙计。谢谢Buuut我的问题是我需要更改原始结构以导入MongoDB。正如您在我的示例中看到的,原始JSON为place
返回一个多维属性,我需要将其重新构造为一级字典(有些字段是可选的)。我认为如果在不同的级别中得到两个同名的键,会有点危险。正当不过还是要谢谢你@izn我根据您的评论对其进行了一些修改,现在您将获得“place_id”和“place_location_city”等密钥