如何将具有多个 JSON 个对象的 Asana API 响应转换为 Python 个对象
How to convert Asana API response with multiple JSON objects to Python objects
我是 Python 的新手。我正在编写一个脚本来发送有关过期 Asana 任务的通知。我正在 运行 解决将 Asana API 响应(具有多个代表任务的对象的 JSON 转换为 Python 对象的问题。现在,我要做的就是将 JSON 对象转换为 Python 对象以验证响应。
这是来自 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"
}
]
}
我的 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"])
但是,当我 运行 它时,我得到以下错误:
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)
如有任何帮助,我们将不胜感激。
您尝试过以下方法吗?将 with
语句替换为以下代码:
with open('./work_in_progress.json') as f:
taskDict = json.load(f)
taskList.append(taskDict)
我是 Python 的新手。我正在编写一个脚本来发送有关过期 Asana 任务的通知。我正在 运行 解决将 Asana API 响应(具有多个代表任务的对象的 JSON 转换为 Python 对象的问题。现在,我要做的就是将 JSON 对象转换为 Python 对象以验证响应。
这是来自 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"
}
]
}
我的 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"])
但是,当我 运行 它时,我得到以下错误:
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)
如有任何帮助,我们将不胜感激。
您尝试过以下方法吗?将 with
语句替换为以下代码:
with open('./work_in_progress.json') as f:
taskDict = json.load(f)
taskList.append(taskDict)