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.

share|improve this question
See this answers stackoverflow.com/questions/3904269/… – powtac Jan 22 '12 at 19:00

6 Answers

up vote 7 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'
       +'}';
share|improve this answer
Or put a \ at the end of each line in the literal. – Phrogz Jan 22 '12 at 19:05

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.

share|improve this answer

Use JSON.stringify:

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

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

share|improve this answer

The way i do it is:

   var obj = new Object();
   obj.name = "Raj";
   obj.age  = 32;
   obj.married = false;
   var jsonString= JSON.stringify(obj);

I guess this way can reduce chances for errors.

share|improve this answer

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.

share|improve this answer
5  
JSON can have line breaks, but JavaScript string literal syntax cannot. – am not i am Jan 22 '12 at 19:03
interally within a string, yeah, but not between key/value pairs. – Marc B Jan 22 '12 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 '12 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 '12 at 19:14

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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