'module' 对象在使用 python 解析 JSON 时没有属性 'loads'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20082730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'module' object has no attribute 'loads' while parsing JSON using python
提问by AKIWEB
I am trying to parse JSON from Python. I recently started working with Python so I followed some stackoverflow tutorial how to parse JSON using Python and I came up with below code -
我正在尝试从 Python 解析 JSON。我最近开始使用 Python,所以我遵循了一些如何使用 Python 解析 JSON 的 stackoverflow 教程,我想出了以下代码 -
#!/usr/bin/python
import json
j = json.loads('{"script":"#!/bin/bash echo Hello World"}')
print j['script']
But whenever I run the above code, I always get this error -
但是每当我运行上面的代码时,我总是收到这个错误 -
Traceback (most recent call last):
File "json.py", line 2, in <module>
import json
File "/cygdrive/c/ZookPython/json.py", line 4, in <module>
j = json.loads('{"script":"#!/bin/bash echo Hello World"}')
AttributeError: 'module' object has no attribute 'loads'
Any thoughts what wrong I am doing here? I am running cygwin in windows and from there only I am running my python program. I am using Python 2.7.3
任何想法我在这里做错了什么?我在 Windows 中运行 cygwin,从那里我只运行我的 python 程序。我正在使用 Python 2.7.3
And is there any better and efficient way of parsing the JSON as well?
还有什么更好更有效的方式来解析 JSON 吗?
Update:-
更新:-
Below code doesn't work if I remove the single quote since I am getting JSON string from some other method -
如果我删除单引号,下面的代码不起作用,因为我从其他方法获取 JSON 字符串 -
#!/usr/bin/python
import json
jsonStr = {"script":"#!/bin/bash echo Hello World"}
j = json.loads(jsonStr)
shell_script = j['script']
print shell_script
So before deserializing how to make sure, it has single quote as well?
那么在反序列化之前如何确保它也有单引号?
This is the error I get -
这是我得到的错误 -
Traceback (most recent call last):
File "jsontest.py", line 7, in <module>
j = json.loads(jsonStr)
File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
采纳答案by SingleNegationElimination
File "json.py", line 2, in <module>
import json
This line is a giveaway: you have named your script "json", but you are trying to import the builtin module called "json", since your script is in the current directory, it comes first in sys.path, and so that's the module that gets imported.
这一行是一个赠品:您已将脚本命名为“json”,但您正在尝试导入名为“json”的内置模块,因为您的脚本位于当前目录中,因此它首先出现在 sys.path 中,所以这就是导入的模块。
You need to rename your script to something else, preferrably not a standard python module.
您需要将脚本重命名为其他名称,最好不是标准的 Python 模块。
回答by Alexander L. Belikoff
It looks like you have a json.pymodule which is not part of the Standard Library. Not sure what ZookPython is. Try renaming ZookPython directory (or just json.py) and re-run.
看起来您有一个json.py不属于标准库的模块。不确定 ZookPython 是什么。尝试重命名 ZookPython 目录(或只是json.py)并重新运行。

