How to create JSON string in JavaScript?

前端 未结 7 1789
借酒劲吻你
借酒劲吻你 2020-12-23 02:31
window.onload = function(){
    var obj = \'{
            \"name\" : \"Raj\",
            \"age\"  : 32,
            \"married\" : false
            }\';

    var va         


        
7条回答
  •  情话喂你
    2020-12-23 03:22

    Javascript doesn't handle Strings over multiple lines.

    You will need to concatenate those:

    var obj = '{'
           +'"name" : "Raj",'
           +'"age"  : 32,'
           +'"married" : false'
           +'}';
    

    You can also use template literals in ES6 and above: (See here for the documentation)

    var obj = `{
               "name" : "Raj",
               "age" : 32,
               "married" : false,
               }`;
    

提交回复
热议问题