window.onload = function(){
    var obj = '{
            "name" : "Raj",
            "age"  : 32,
            "married" : false
            }';

    var val = eval('(' + obj + ')');
    alert( "name : " + val.name + "\n" +
           "age  : " + val.age  + "\n" +
           "married : " + val.married );

}

In a code something like this, I am trying to create jsonstring just to play around. Its throwing error but if I put all the name, age, married in one single line (line 2) It doesn't. Whats the problem.

link|improve this question

69% accept rate
See this answers stackoverflow.com/questions/3904269/… – powtac Jan 22 at 19:00
feedback

5 Answers

up vote 4 down vote accepted

Javascript doesn't handle Strings over multiple lines.

You will need to concatenate those:

var obj = '{'
       +'"name" : "Raj",'
       +'"age"  : 32,'
       +'"married" : false'
       +'}';
link|improve this answer
Or put a \ at the end of each line in the literal. – Phrogz Jan 22 at 19:05
feedback

Use JSON.stringify:

> JSON.stringify({ asd: 'bla' });
'{"asd":"bla"}'
link|improve this answer
1  
Good advice, but it doesn't show what the problem is. – Phrogz Jan 22 at 19:04
See also json2.js if you need to support older browsers. – Douglas Jan 22 at 19:05
@Douglas is this Douglas Crockford ? Just curious??? – nepsdotin Jan 22 at 19:27
Yes, here's a link to his GitHub project list: github.com/douglascrockford – Douglas Jan 22 at 19:43
So excited to see your answer, Thank You Very much Sir. – nepsdotin Jan 22 at 19:55
show 2 more comments
feedback

json strings can't have line breaks in them. You'd have to make it all one line: {"key":"val","key2":"val2",etc....}.

But don't generate JSON strings yourself. There's plenty of libraries that do it for you, the biggest of which is jquery.

link|improve this answer
3  
JSON can have line breaks, but JavaScript string literal syntax cannot. – am not i am Jan 22 at 19:03
interally within a string, yeah, but not between key/value pairs. – Marc B Jan 22 at 19:07
I think you're confusing JavaScript string literal syntax which cannot contain an unescaped newline character, and JSON markup. JSON markup can most certainly contain line breaks. – am not i am Jan 22 at 19:11
...paste the code in your question into jsonlint.com (without the etc.... of course). After clicking Validate, you'll see that it actually inserts newlines when it pretty-prints. – am not i am Jan 22 at 19:14
feedback

There is a "official" JSON implementation http://www.json.org/js.html by Crockford: https://github.com/douglascrockford/JSON-js

link|improve this answer
feedback

The function JSON.stringify will turn your json object into a string:

var jsonAsString = JSON.stringify(obj);

In case the browser does not implement it (IE6/IE7), use the JSON2.js script. It's safe as it uses the native implementation if it exists.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.