0

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));

3 Answers 3

1
{
    "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/

Sign up to request clarification or add additional context in comments.

1 Comment

+1 since your example is correct, you provide a nice tool to validate. Thanks!
0

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>\"]}"

1 Comment

Thanks for the explanation, the second example solved my problem
0
var data = '{"29-05-yyyy": "<li><divclass=\'vazio\'></div><div class=\'linha\'><span>Parabéns!</span></div><divclass=\'vazio\'></div></li>"}';

1 Comment

Please add some explanation regarding how your solution answers the question.

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.