简体   繁体   English

如何将带有多个 JSON 对象的 Asana API 响应转换为 Python 对象

[英]How to convert Asana API response with multiple JSON objects to Python objects

I'm new to Python.我是 Python 的新手。 I'm working on a script to send notifications on overdue Asana tasks.我正在编写一个脚本来发送有关过期 Asana 任务的通知。 I'm running into issues with converting the Asana API response, which is a JSON with multiple objects that represent tasks, to Python objects.我在将 Asana API 响应(具有多个表示任务的对象的 JSON)转换为 Python 对象时遇到问题。 For now, all I want to do is convert the JSON objects into Python objects to validate the response.现在,我要做的就是将 JSON 对象转换为 Python 对象以验证响应。

This is what the raw JSON response from Asana looks like:这是来自 Asana 的原始 JSON 响应的样子:

{
    "data": [
      {
        "gid": "1234567891234567",
        "due_on": "2021-02-12",
        "name": "My First Task",
        "permalink_url": "https://app.asana.com/0/123456789/1234567891234567"
      },
      {
        "gid": "1234567891234568",
        "due_on": "2021-02-26",
        "name": "My Second Task",
        "permalink_url": "https://app.asana.com/0/123456789/1234567891234568"
      },
      {
        "gid": "1234567891234569",
        "due_on": null,
        "name": "My Third Task",
        "permalink_url": "https://app.asana.com/0/123456789/1234567891234569"
      }
    ]
  }

My Python code looks like the following:我的 Python 代码如下所示:

import json
import asana


taskList = []
with open('./work_in_progress.json') as f:
    for jsonObj in f:
        taskDict = json.loads(jsonObj)
        taskList.append(taskDict)

print("Printing each JSON Decoded Object")
for task in taskList:
    print(task["gid"], task["name"], task["due_on"], task["permalink"])

However, when I run it, I get the following error:但是,当我运行它时,我收到以下错误:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    taskDict = json.loads(jsonObj)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting object: line 1 column 2 (char 1)

Any help would be appreciated.任何帮助,将不胜感激。

Have you tried the following?您是否尝试过以下操作? Replace the with statement with this code:用以下代码替换with语句:

with open('./work_in_progress.json') as f:
    taskDict = json.load(f)
    taskList.append(taskDict)

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

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