How to create JSON string in JavaScript?

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

    var va         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 03:19

    This can be pretty easy and simple

    var obj = new Object();
    obj.name = "Raj";
    obj.age = 32;
    obj.married = false;
    
    //convert object to json string
    var string = JSON.stringify(obj);
    
    //convert string to Json Object
    console.log(JSON.parse(string)); // this is your requirement.
    

提交回复
热议问题