我在 PHP 中创建了一个 JSON 对象,该 JSON 对象在其中一个单元格中包含另一个转义的 JSON 字符串:
php > $insidejson = array('foo' => 'bar','foo1' => 'bar1'); php > $arr = array('a' => array('a1'=>json_encode($insidejson))); php > echo json_encode($arr); {"a":{"a1":"{\"foo\":\"bar\",\"foo1\":\"bar1\"}"}}
然后,使用 Python,我尝试使用 simplejson 对其进行解码:
>>> 将 simplejson 导入为 json >>> json.loads('{"a":{"a1":"{\"foo\":\"bar\",\"foo1\":\"bar1\"}"}}')
这失败并出现以下错误:
回溯(最近一次通话最后): 文件“”,第 1 行,在? 加载中的文件“build/bdist.linux-i686/egg/simplejson/__init__.py”,第 307 行 解码中的文件“build/bdist.linux-i686/egg/simplejson/decoder.py”,第 335 行 文件“build/bdist.linux-i686/egg/simplejson/decoder.py”,第 351 行,在 raw_decode ValueError: Expecting , delimiter: line 1 column 14 (char 14)
如何在 Python 中解码这个 JSON 对象?PHP 和 JS 都成功解码了它,我不能改变它的结构,因为这需要用不同的语言对许多不同的组件进行重大更改。
谢谢!