开发者

Python read multiline JSON

I have been trying to use JSON to store settings for a program. I can't seem to get Python 2.6 's JSON Decoder to decode multi-line JSON strings...

Here is example input:

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

I have tried a couple other syntaxes for this file, which I will specify below (with the traceback they cause).

My python code for reading the file in is

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

The Traceback for this is:

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)

I assume the "Extra data" is the triple-quote.

Here are the other syntaxes I have tried for the .settings file, with their respective 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,开发者_如何学JAVA 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

If I put the settings all on one line, it decodes fine.


Get rid of all of the backslashes and all of the "Pythonic" quoting in the settings file. Works fine if the file is just:

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

Note also that JSON strings are quoted with double quotes, not single quotes. See JSON spec here:

http://www.json.org/


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

json doesn't consider \ to be a line-continuation character.


Try to use eval(s)

s=""" {\ 'user':'username',\ 'password':'passwd',\ \ """

ss=eval(q)

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

type(qq) dict

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜