views:

1021

answers:

5

Hi,

My JSON response looks like:

{rc: "200", test: "", user: "<div class='sub1'>
                <div class='avatar'>                    
                    <a href='/blah'>blah</a><br />
                    <strong>0</strong>
                </div>
                <div class='sl'>
                    <p>
                        you droppped the ball this time
                    </p>
                </div>
                <div class='clear'>
                </div>                        
            </div>"}

Update

I updated my code to put quotes on the key values, still doesn't work:

{"rc": "200", "m" : "", "o": "<div class='s1'>
            <div class='avatar'>                    
                <a href='\/asdf'>asdf<\/a><br \/>
                <strong>0<\/strong>
            <\/div>
            <div class='sl'>
                <p>
                    444444444
                <\/p>
            <\/div>
            <div class='clear'>
            <\/div>                        
        <\/div>"}
A: 

You don't need to escape HTML in a javascript string. What exactly are you trying to do/what is the problem? Take a look at the escape() function - it might help.

nickf
The badly named escape() function is for URI encoding, and what is needed here is JavaScipt string escaping of the quote (either single or double depending out outer quote, and the backslash).
Kevin Hakanson
There are no double quotes in his example string, and if there were, using escape would convert " to %22 which would avoid any problems with breaking the string. You could then easily use unescape() to bring it back to a readable format.
nickf
But, since JSON is JavaScript object literal notation, the string decoding rules are backed on backslash escaping, not URI escaping. Why add another layer of custom escaping that has to be done by both sides, when the built in rules will work?
Kevin Hakanson
well given that the OP said there was a problem, but there was no error in the code sample, I was just trying to guess what might be the problem.
nickf
+1  A: 

nothing in this example.

Itay Moav
+1  A: 

In your example, you won't have to escape anything. But, if the HTML comes with double quotes, you'll have to escape them, obviously.

Julio Greff
Double-quotes and backslash characters. "C:\xyz\" will break things.
Samir Talwar
Yes, you're right. Thanks for remembering me =)
Julio Greff
A: 

You're HTML values are OK, but the keys of the JSON object must be enclosed in quotes.

From the JSON RFC:

2.2. Objects

An object structure is represented as a pair of curly brackets
surrounding zero or more name/value pairs (or members). A name is a
string.

and

2.5. Strings

The representation of strings is similar to conventions used in the C
family of programming languages. A string begins and ends with
quotation marks.

Also, if you output this JSON object inside the script tags of an HTML page, you must escape the "</" sequence of HTML closing tags, as per this appendix in the HTML 4 specification.

Ionuț G. Stan
Ok I updated my code, does that look like it should be working?
mrblah
+2  A: 

I used jsonlint to validate your latest example, and the line breaks are what it flagged. When they were removed, it validated.

{
    "rc": "200",
    "m" : "",
    "o": "<div class='s1'><div class='avatar'><a href='\/asdf'>asdf<\/a><br \/><strong>0<\/strong>      <\/div>    <div class='sl'><p>444444444<\/p><\/div><div class='clear'><\/div><\/div>"
}
Kevin Hakanson
How can I escape line breaks programatically?
mrblah