2010-05-26 60 views
13

我一直在尝试使用JSON来存储程序的设置。我似乎无法得到的Python 2.6的JSON解码器进行解码的多线串JSON ...Python阅读多行JSON

这里是例如输入:

.settings file: 
""" 
{\ 
    'user':'username',\ 
    'password':'passwd',\ 
}\ 
""" 

我已经尝试了一些其他的语法此文件,我将在下面指出(它们引起的回溯)。读取文件中的

我的Python代码是

import json 
settings_text = open(".settings", "r").read() 
settings = json.loads(settings_text) 

回溯这就是:

Traceback (most recent call last): 
    File "json_test.py", line 4, in <module> 
    print json.loads(text) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py", line 307, in loads 
    return _default_decoder.decode(s) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 322, in decode 
    raise ValueError(errmsg("Extra data", s, end, len(s))) 
ValueError: Extra data: line 1 column 2 - line 7 column 1 (char 2 - 41) 

我承担“额外数据”三重引号。

下面是其他语法我都试过了.settings文件,用各自的回溯:

"{\ 
    'user':'username',\ 
    'pass':'passwd'\ 
}" 

Traceback (most recent call last): 
    File "json_test.py", line 4, in <module> 
    print json.loads(text) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py", line 307, in loads 
    return _default_decoder.decode(s) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 319, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 336, in raw_decode 
    obj, end = self._scanner.iterscan(s, **kw).next() 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/scanner.py", line 55, in iterscan 
    rval, next_pos = action(m, context) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 155, in JSONString 
    return scanstring(match.string, match.end(), encoding, strict) 
ValueError: Invalid \escape: line 1 column 2 (char 2) 



'{\ 
    "user":"username",\ 
    "pass":"passwd",\ 
}' 

Traceback (most recent call last): 
    File "json_test.py", line 4, in <module> 
    print json.loads(text) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py", line 307, in loads 
    return _default_decoder.decode(s) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 319, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 338, in raw_decode 
    raise ValueError("No JSON object could be decoded") 
ValueError: No JSON object could be decoded 

如果我把所有的设置在同一行,其解码罚款。

回答

20

摆脱设置文件中的所有反斜杠和所有“Pythonic”引用。工作正常,如果文件是:

{ 
    "user":"username", 
    "password":"passwd" 
} 

请注意,JSON字符串用双引号引起来,而不是单引号。见JSON规范在这里:

http://www.json.org/

2
>>> s = """ 
{ 
    "user":"username", 
    "password":"passwd" 
} 
""" 
>>> json.loads(s) 
{'password': 'passwd', 'user': 'username'} 

json不认为\是一个换行字符。

+0

这并不工作,当您创建的字符串解释器,但从文件读取时不起作用。 – 2010-05-26 14:01:50

+1

@保罗:因为你的文件内容不正确! – SilentGhost 2010-05-26 14:07:15

-5

尝试使用eval(S)

S = “”” {\ '用户': '用户名',\ 'password':'passwd',\ \ “”“

ss = eval(q)

QQ { '密码': 'passwd文件', '用户': '用户名'}

类型(QQ) 字典

+5

请不要使用eval - http://stackoverflow.com/questions/661084/security-of-pythons-eval-on-untrusted-strings/661128#661128 – 2013-01-05 09:31:45