I am trying to encode a Python array in json using simplejson.dumps:
In [30]: s1 = ['test', '<script>'] In [31]: simplejson.dumps(s1) Out[31]: '["test", "<script>"]'
It works great.
But first, I want to avoid the lines (using escapejs from Django) before calling simplejson.dumps:
In [35]: s_esc Out[35]: [u'test', u'\\u003Cscript\\u003E'] In [36]: print simplejson.dumps(s_esc) ["test", "\\u003Cscript\\u003E"]
My problem: I want the escaped string to be: ["test", "\u003Cscript\u003E"]
instead of ["test", "\\u003Cscript\\u003E"]
I can use replace
:
In [37]: print simplejson.dumps(s_esc).replace('\\\\', '\\') ["test", "\u003Cscript\u003E"]
But is this a good approach? I just want to avoid strings before their json encoding. Therefore, when I use them in the template, there will be no syntax errors.
Thanks.:)
source share