简体   繁体   English

调用 JSON 对象的“from”属性时出现语法错误

[英]Syntax error when calling "from" attribute of a JSON object

I am using RingCentral API to get call log information.我正在使用 RingCentral API 来获取通话记录信息。

I am interested in an attribute called from which tells us who was the caller who made the call.我对调用from的属性感兴趣,它告诉我们谁是拨打电话的来电者。 To access the caller name, I need to access a call record -> then go to the last leg (root of the call) and then get the from attribute, and then get the name of the caller.要访问来电者姓名,我需要访问通话记录 -> 然后转到最后一段(通话的根)然后获取 from 属性,然后获取来电者的姓名。

Eg:例如:

"from": { "name": "andy murray", "phoneNumber": "+44712345656" } "from": { "name": "andy murray", "phoneNumber": "+44712​​345656" }

resp = platform.get('/restapi/v1.0/account/~/call-log', params)       
for record in resp.json().records:
    print(record.legs[-1].from.name)

After running this command, I am getting error:运行此命令后,我收到错误:

      File "demo.py", line 43
        print(record.legs[-1].from.name )
SyntaxError: invalid syntax

It looks like Python is not able to comprehend that this "from" is not part of Python itself.看起来 Python 无法理解这个“来自”不是 Python 本身的一部分。 How can I define Python to consider this as an attribute of a JSON object?如何定义 Python 以将其视为 JSON 对象的属性?

Any help will be appreciated!任何帮助将不胜感激!

Use __dict__ to get all attributes of object.使用__dict__获取对象的所有属性。 https://stackoverflow.com/a/39392891/9024042 https://stackoverflow.com/a/39392891/9024042

As mentioned in the answer of the attached post.正如所附帖子的答案中所述。 I called record.legs[-1].__dict__ and found out all attributes of json object.我调用了record.legs[-1].__dict__并找出了 json 对象的所有属性。

I found that, there was no attribute named "from", It was "from_".我发现,没有名为“from”的属性,它是“from_”。

I hope it helps someone with the same issue我希望它可以帮助有同样问题的人

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

相关问题 从文件调用属性时的语法错误 - Syntax error when calling attributes from a file 从自定义模块中追加获得的列表中调用列表对象时出现“Int object has no attribute”错误 - "Int object has no attribute" error when calling list object from list obtained by append in custom module 调用函数时的语法错误 - syntax error when calling a function 错误 AttributeError: 'function' object has no attribute 'json' occures when importing functions from other files - Error AttributeError: 'function' object has no attribute 'json' occures when importing functions from other files 调用 scipy function 时出现属性错误 - Attribute error when calling calling a scipy function 从另一个文件调用 function 时出现 Python 属性错误 - Python attribute error when calling a function from another file Tkinter:按下按钮时调用 function 但出现属性错误“应用程序”object 没有属性“嗨” - Tkinter: Calling function when button is pressed but i am getting attribute error 'Application' object has no attribute 'hi' 'str' object 没有属性/调用字典时出错 - 'str' object has no attribute / error calling a dictionary 在Anaconda中调用github包时的语法错误 - Syntax error when calling github package in anaconda 从 Python 调用 bash 脚本时出现“语法错误:未终止的引用字符串”错误 - “Syntax error: Unterminated quoted string” error when calling a bash script from Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM