处理来自Facebook API的响应

时间:2019-04-17 14:22:07

标签: python json dictionary

具有Facebook Graph API的响应:

    "data": [
        {
         "name": "page_impressions",
         "period": "day",
         "values": [
            {
               "value": 0,
               "end_time": "2019-04-16T07:00:00+0000"
            },
            {
               "value": 0,
               "end_time": "2019-04-17T07:00:00+0000"
            }
         ],
         "title": "Daily Total Impressions",
         "description": "Daily: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, check-ins, ads, social information from people who interact with your Page and more. (Total Count)",
        },
    {
      "name": "page_fan_adds",
      "period": "day",
      "values": [
        {
          "value": 0,
          "end_time": "2019-04-16T07:00:00+0000"
        },
        {
          "value": 0,
          "end_time": "2019-04-17T07:00:00+0000"
        }
      ],
      "title": "Daily New Likes",
      "description": "Daily: The number of new people who have liked your Page (Total Count)",
      "id": "145648302731807/insights/page_fan_adds/day",
    }]}

直到现在我的代码:

def manipulate(result):
    dates = []
    if "data" in result:
        dates = result['data']

    entriesToRemove = {'description', 'title', 'period', 'id'}
    for element in dates:
        for entries in entriesToRemove:
            element.pop(entries, False)

    return dates

...将返回: [{'values': [{'end_time': '2019-04-14T07:00:00+0000', 'value': 0}, {'end_time': '2019-04-15T07:00:00+0000', 'value': 0}], 'name': 'page_impressions'}, {'values': [{'end_time': '2019-04-16T07:00:00+0000', 'value': 0}, {'end_time': '2019-04-17T07:00:00+0000', 'value': 0}], 'name': 'page_fan_adds'}]

...我想将其处理为最终形式:

[{
    "page_impressions": 0,
    "end_time": "2019-04-16T07:00:00+0000",
    },
  {
    "page_impressions": 0,
    "end_time": "2019-04-17T07:00:00+0000",
    },
{
    "page_fan_adds": 0,
    "end_time": "2019-04-16T07:00:00+0000",
    },
{
    "page_fan_adds": 0,
    "end_time": "2019-04-17T07:00:00+0000",
    },
]

我应该如何处理这种情况?我不知道如何处理响应。也许还有另一种方式可以做到这一点...

2 个答案:

答案 0 :(得分:0)

假设您的dict被命名为d

my_list = []

for item in d["data"]:
    for value in item["values"]:
        new_dict = dict()
        new_dict[item["name"]] = value["value"]
        new_dict["end_time"] = value["end_time"]
        my_list.append(new_dict)
for element in my_list:
    print(element)

请注意,此解决方案将不会进行适当的操作。相反,它将采用所需的元素并创建一个新对象。速度为O(n),可读性很强。

答案 1 :(得分:0)

result = { "data": [
        {
         "name": "page_impressions",
         "period": "day",
         "values": [
            {
               "value": 0,
               "end_time": "2019-04-16T07:00:00+0000"
            },
            {
               "value": 0,
               "end_time": "2019-04-17T07:00:00+0000"
            }
         ],
         "title": "Daily Total Impressions",
         "description": "Daily: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, check-ins, ads, social information from people who interact with your Page and more. (Total Count)",
        },
    {
      "name": "page_fan_adds",
      "period": "day",
      "values": [
        {
          "value": 0,
          "end_time": "2019-04-16T07:00:00+0000"
        },
        {
          "value": 0,
          "end_time": "2019-04-17T07:00:00+0000"
        }
      ],
      "title": "Daily New Likes",
      "description": "Daily: The number of new people who have liked your Page (Total Count)",
      "id": "145648302731807/insights/page_fan_adds/day",
    }]}   



def manipulate(result):
    dates_result = [] 
    if "data" in result:
        dates = result['data']  
    entriesToRemove = {'description', 'title', 'period', 'id'}
    for element in dates:
        for entries in entriesToRemove:
            element.pop(entries, False)

        for each in element['values']:
            dates_result.append({element['name']:each['value'], 'end_time':each['end_time']})
    return dates_result


manipulated_results = manipulate(result)

输出:

print (manipulated_results)
[{'page_impressions': 0, 'end_time': '2019-04-16T07:00:00+0000'}, {'page_impressions': 0, 'end_time': '2019-04-17T07:00:00+0000'}, {'page_fan_adds': 0, 'end_time': '2019-04-16T07:00:00+0000'}, {'page_fan_adds': 0, 'end_time': '2019-04-17T07:00:00+0000'}]