-4

I'm simply trying to set this JSON string to a variable but I'm not doing something right, not escaping something right

    var stringJson= '{
                                    "Status": {
                                        "Code": 3002,
                                        "Message": "something",
                                        "Succeeded": false
                                    }
}'

it's not liking the brackets, doesn't treat it as a string and is treating it as actual js code

PositiveGuy
  • 46,620
  • 110
  • 305
  • 471
  • what's between the single quotes isn't JS though... are there any errors in the console? – Don Oct 29 '14 at 18:14
  • 3
    You can't have line breaks in a string. Why are you creating JSON that way in the first place? –  Oct 29 '14 at 18:15
  • actually you're missing a `}` at the end there, then it's valid JSON – Don Oct 29 '14 at 18:16
  • This is for documentation, I don't have the js object, I'm going to be showing code for a doc site...and I copied this json from another doc site and statically want to show it as is with indentation and all that. – PositiveGuy Oct 29 '14 at 18:17
  • I'm not looking for valid json, I just posted this half assed. – PositiveGuy Oct 29 '14 at 18:18
  • dude to make you happy I updated it. The code I pasted FROM is correct, I simply pasted half of it in here and forgot to complete it. – PositiveGuy Oct 29 '14 at 18:18
  • @CoffeeAddict make who happy? we're trying to help, make sure it's valid – Don Oct 29 '14 at 18:19
  • I am not loooking for people to validate the json, I'm asking how can I set this variable to the static json string – PositiveGuy Oct 29 '14 at 18:19
  • @CoffeeAddict you already did that in your post – Don Oct 29 '14 at 18:20
  • @CoffeeAddict: If you don't care about it being valid JSON, then don't say you need JSON. If you want to know what's wrong with the string, I told you in my comment above. –  Oct 29 '14 at 18:21
  • *"...doesn't treat it as a string and is treating it as actual js code..."* A string *is* actual JS code. –  Oct 29 '14 at 18:23
  • @squint code that does nothing at all? – Don Oct 29 '14 at 18:28
  • @Blaine: Yes? A string literal is no less JavaScript code than an object literal, and broken string literal syntax is broken JS code just as broken object literal syntax would be. –  Oct 29 '14 at 18:45
  • @squint http://stancarey.files.wordpress.com/2014/01/stan-carey-indo-european-jones-meme-whyd-it-have-to-be-semantics.jpg – Don Oct 29 '14 at 19:34
  • there updated the title to make more sense – PositiveGuy Oct 29 '14 at 19:52

1 Answers1

3

1) you were missing the closing bracket for "Status",

2) You cannot have line breaks in javascript strings without escaping them:

var stringJson= '{\
                     "Status": {\
                         "Code": 3002,\
                         "Message": "something",\
                         "Succeeded": false\
                      }\
                 }';

Better yet you should just create an object and JSON.stringify it:

var obj = { Status : { Code : 3002, Message : 'something', Succeeded : false } };
var stringJson = JSON.stringify(obj);
Jonathan Crowe
  • 5,793
  • 1
  • 18
  • 28