Python读取多行JSON

时间:2010-05-26 13:52:08

标签: python json

我一直在尝试使用JSON来存储程序的设置。我似乎无法使用Python 2.6的JSON解码器来解码多行JSON字符串...

以下是示例输入:

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

我为此文件尝试了其他一些语法,我将在下面指定(使用它们引起的回溯)。

用于读取文件的python代码是

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

Traceback的目的是:

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文件尝试的其他语法及其各自的Tracebacks:

"{\
  '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

如果我将设置全部放在一行上,则解码很好。

3 个答案:

答案 0 :(得分:20)

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

,则工作正常
{
  "user":"username",
  "password":"passwd"
}

另请注意,JSON字符串引用双引号,而不是单引号。请参阅JSON规范:

http://www.json.org/

答案 1 :(得分:2)

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

json不认为\是一个行继续符。

答案 2 :(得分:-5)

尝试使用eval(s)

  
    
      

S = “””       {\         '用户': '用户名',\         '密码': 'passwd文件',\       \       “”“

             

SS = EVAL(Q)

             

QQ        {'password':'passwd','user':'username'}

             

型(QQ)        字典