Ardalan Shahgholi
Ardalan Shahgholi

Reputation: 12555

eval() works but JSON.parse() and $.parseJSON() doesn't work

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.

Upvotes: 0

Views: 703

Answers (1)

You
You

Reputation: 54

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.

Upvotes: 3

Related Questions