2

GAE 上的 django.utils.simplejson 版本例如转义“/”字符,但不是“\n”,js = json.dumps(my_dict_w_strings_w_newline_and_slash)当我尝试json.loads(js)在我的客户端其他地方时会导致问题。

有关如何解决解决方案的任何建议?字符串是 base64 编码的数据,因此会被破坏。

4

2 回答 2

3

我尝试了 SDK 附带的 simplejson 版本(Django 0.96 和 1.2)并且都转义了 '\n':

>>> simplejson.dumps({'foo': '\n'})
'{"foo": "\\n"}'

http://shell.appspot.com/上:

Google App Engine/1.5.1
Python 2.5.2 (r252:60911, Mar 17 2011, 15:16:30) 
[GCC 4.3.1]

>>> from django.utils import simplejson
>>> simplejson.dumps({'foo': '\n'})
'{"foo": "\\n"}'
>>> simplejson.dumps('foo/bar')
'"foo\\/bar"'
于 2011-06-22T23:21:06.030 回答
0

我的同事建议:

if json.encoder.ESCAPE_DCT.get('/') != '/':
    json.encoder.ESCAPE_DCT['/'] = '/'

效果很好。

于 2011-06-23T06:49:06.910 回答