道歉,如果這是一個重複或簡單的問題。OOP - 訪問對象數據
我最近一直在學Python(花了很多年編寫簡單的MATLAB腳本)。我已經開始探索麪向對象編程和JSON。
我想使用API從服務器收集數據。當對象返回時,我通常使用語法來訪問特定的數據字段。不過,我正在爲此苦苦掙扎。我有一個行對象:
row = {"totalCount": 1, "results": [{"parentObjectId": 887, "contextData": ["Row 1"], "parentObjectType": "sheet", "objectId": 599, "text": "Text", "parentObjectName": "Data", "objectType": "row"}]}
我試圖訪問「OBJECTID」屬性的單個結果(result[0]
)。
我試過rowId = row.results[0].objectId
,但得到錯誤「'SearchResultItem'對象沒有屬性'objectId'」。
我也試過rowId = row.results[0]['objectId']
,但得到錯誤「'SearchResultItem'對象沒有屬性'__getitem__
'」。
---編輯:
print(reportingRow.results[0]['objectId'])
Traceback (most recent call last):
File "<ipython-input-46-14e026c273e3>", line 1, in <module>
print(reportingRow.results[0]['objectId'])
TypeError: 'SearchResultItem' object has no attribute '__getitem__'
我使用了一個名爲Smartsheet工具。我正在使用search_sheet請求。 API文檔(http://smartsheet-platform.github.io/api-docs/#search-sheet)說'SearchResultItem'是一個包含許多屬性的對象。它沒有提供更多的信息。
Smartsheet模型在這裏找到:https://github.com/smartsheet-platform/smartsheet-python-sdk/tree/master/smartsheet/models。我目前正在查找search_result.py和search_result_item.py來查找答案/線索。
---編輯結束
感謝您的幫助!
你的對象不是一本字典(它只是當你打印它時看起來像的東西)。你的錯誤清楚地表明它是一個'SearchResultItem'。我們不知道'SearchResultItem'實現了什麼樣的接口,因此我們無法幫到你。 –
讓我們知道什麼是'SearchResultItem',它不是一個json字符串,也不是一個字典。 – tianwei
請提供從API獲取數據的全部響應,然後讓我們知道哪裏存在問題。 –