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.

Ardalan Shahgholi
  • 11,967
  • 21
  • 108
  • 144

1 Answers1

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.

You
  • 54
  • 2
  • moreover, the object keys *must* be quoted using doublequotes – Lud May 31 '16 at 14:42
  • 4 up-votes so far for an answer that suggests that JavaScript/JSON syntax is a matter of browser kindness? ;-P – Álvaro González Jun 01 '16 at 12:28
  • 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. – You Jun 04 '16 at 17:38
  • 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. – Álvaro González Jun 06 '16 at 14:35