Pluda
Pluda

Reputation: 1477

Jquery JSON.parse error

I need to parse a php received string to behave like a object literal. I'm trying with json.parse but I got this error "JSON Parse error: Expected ']'". I'm not seeing why, can you please have a look to this code?

Thanks

Note this data variable is a copy/paste from what php returns just for testing

var data = "{\"29-05-yyyy\":[\"<li><div class=\"vazio\"></div><div class=\"linha\"><span>Parabéns!</span></div><div class=\"vazio\"></div></li>\"]}";
alert(JSON.parse(data));

Upvotes: 0

Views: 156

Answers (3)

distvan
distvan

Reputation: 1

var data = '{"29-05-yyyy": "<li><divclass=\'vazio\'></div><div class=\'linha\'><span>Parabéns!</span></div><divclass=\'vazio\'></div></li>"}';

Upvotes: 0

4EACH
4EACH

Reputation: 2197

{
    "29-05-yyyy": [
        "<li><div class=\"vazio\"></div><divclass=\"linha\"><span>Parabéns!</span></div><div    class=\"vazio\"></div></li>"
    ]
}

You have to place all the backslashes before inner double quotes,
not before key or value double quotes.
Valid your json here: http://jsonlint.com/

Upvotes: 1

John S
John S

Reputation: 21492

The problem is the quotes for the html attributes are not properly escaped.

Your JSON basically looks like this:

{"29-05-yyyy":["<li><div class="vazio"></div>...
                               ^ this ends the string

You need to doubly escape the quotes for the html attributes, like this:

"{\"29-05-yyyy\":[\"<li><div class=\\\"vazio\\\"></div><div class=\\\"linha\\\"><span>Parabéns!</span></div><div class=\\\"vazio\\\"></div></li>\"]}"

Alternatively, you could use single quotes for the html attributes, like this:

"{\"29-05-yyyy\":[\"<li><div class='vazio'></div><div class='linha'><span>Parabéns!</span></div><div class='vazio'></div></li>\"]}"

Upvotes: 0

Related Questions