Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:';str';对象没有属性';装载';,json.loads()_Python_Json - Fatal编程技术网

Python 属性错误:';str';对象没有属性';装载';,json.loads()

Python 属性错误:';str';对象没有属性';装载';,json.loads(),python,json,Python,Json,片段 import json teststr = '{"user": { "user_id": 2131, "name": "John", "gender": 0, "thumb_url": "sd", "money": 23, "cash": 2, "material": 5}}' json = json.load(teststr) 抛出异常 Traceback (most recent call last): File "<input>", lin

片段

    import json
    teststr = '{"user": { "user_id": 2131, "name": "John", "gender": 0,  "thumb_url": "sd", "money": 23, "cash": 2, "material": 5}}'
    json = json.load(teststr)
抛出异常

Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'str' object has no attribute 'loads'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“str”对象没有“loads”属性
如何解决问题?

接收一个文件指针,然后传入一个字符串。您可能打算使用接收字符串作为其第一个参数的


其次,当您导入json时,您应该注意不要覆盖它,除非它完全是有意的:
json=json.load(teststr)
您能提供更多详细信息吗?这是JS?你能给被剪断的链接吗?尝试加载而不是加载错误消息与你的示例不匹配
json.loads
永远不会作为直接输入的一部分调用。您是否碰巧将JSON数据字符串绑定到名称
JSON
?它应该是
.loads()
。尝试使用that.json=json.load(teststr)=>out_json=json.loads(teststr)并再次测试。我最喜欢的是最新版本,因为它是最清晰和限制性最强的(即使用您不会使用或意外覆盖的函数污染全局命名空间),我实际上也有同样的问题,我使用的是json.loads
import json
teststr = '{"user": { "user_id": 2131, "name": "John", "gender": 0,  "thumb_url": "sd", "money": 23, "cash": 2, "material": 5}}'
json_obj = json.loads(teststr)
import json as JSON
teststr = '{"user": { "user_id": 2131, "name": "John", "gender": 0,  "thumb_url": "sd", "money": 23, "cash": 2, "material": 5}}'
json = JSON.loads(teststr)
from json import loads
teststr = '{"user": { "user_id": 2131, "name": "John", "gender": 0,  "thumb_url": "sd", "money": 23, "cash": 2, "material": 5}}'
json = loads(teststr)