0

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

4
  • Can you describe how jquery doesn't understand it or what error you got when you use this json in jquery ? Commented Mar 21, 2012 at 13:03
  • Why two backslashes before the forward slash? Surely that means one escaped backslash and one unescaped forward slash? Commented Mar 21, 2012 at 13:32
  • 1
    Can you show us the jQuery code? I tried JSON.parse('{"data":"http://abc/def"}') in my browser's web console and it worked fine. Commented Mar 21, 2012 at 13:59
  • @Marcin: I didnt use editor code tag at first Commented Mar 21, 2012 at 15:14

1 Answer 1

5

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("/", "\\/")
Sign up to request clarification or add additional context in comments.

1 Comment

If you are not using a custom library that messes up the forward slashes, there is something wrong. You should consider locating the bug as it will create more and more problems as your application grows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.