user1248092
user1248092

Reputation: 371

Python JSON encoder

I have a dict like this:

data = {"data":"http://abc/def"}

when I call json.dumps(data) I get this:

'{"data":"http://abc/def"}'

but I want this:

'{"data":"http:\/\/abc\/def"}'

because I use jquery to parse json but seems like it don't understand unescaped solidus, or is there any way to make jquery understand?

UPDATE

For example, here is my json data

{"data": ["http://abc.com/aaaaaaaa/bbbbbbbbb/cccccccccc/xyz.mp3"]}

Here is my success function

function showResult(result) {
            $.each(result.data, function(i, item){
                link = $('<a>').attr('href', item).text(item)
                $("#result").append('<br>')
                $("#result").append(link);
            });
        }

The result should be a hyperlink to

http://abc.com/aaaaaaaa/bbbbbbbbb/cccccccccc/xyz.mp3

But I got a hyperlink to

http&#58;&#47;&#47;abc.com&#47;aaaaaaaa&#47;bbbbbbbbb&#47;cccccccccc&#47;xyz.mp3

If replace all '/' by '\/', everything is fine

Upvotes: 0

Views: 1090

Answers (1)

orlp
orlp

Reputation: 117681

Normally you don't escape forward slashes in JSON, but if you are certain this is your problem you can simply do this:

s = json.dumps(data)
s = s.replace("/", "\\/")

Upvotes: 5

Related Questions