0

I have a string returned from the server in my object property : (R.Message)

    "{
        error:{
            titleName:"||BroadcastableException_Title",
            errorDescriptionName:"||BroadcastableException_Description",
            errorMessageParameters:"",
            message:"\u003cb\u003e\u003ci\u003eBroadcast\u0027s status has been changed \u003c/i\u003e\u003c/b\u003e",
            stackTrace:" XXXX\\XXX.asmx.cs:line 851"
        }
    }"

I can use

(eval('(' + R.Message + ')')

But JSON.parse dosen't work

JSON.parse(R.Message)

I have this error :

Uncaught Invalid JSON: {error: {titleName:"||BroadcastableException_Title",errorDescrip .

Why Eval function can create object from this string but JSOn.parse can't?

I tried $parseJSON and I got the same error.

2

1 Answer 1

3

In JSON, object keys are type "string", and thus need to be quoted. Your keys are unquoted.

Your web browser is kind enough to allow unquoted strings.

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

4 Comments

moreover, the object keys must be quoted using doublequotes
4 up-votes so far for an answer that suggests that JavaScript/JSON syntax is a matter of browser kindness? ;-P
Alvaro: I can't tell what you're getting at. If you think the score is too low or high, vote. If you don't think my answer is very good, write a better one.
I'll clarify: both JSON and JavaScript are languages with a clearly defined syntax and, unlike HTML, the browser mustn't attempt to fix and run code that's syntactically wrong but throw a parse error and abort.

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.