Converting an object to a string

后端 未结 30 1868
北荒
北荒 2020-11-22 03:29

How can I convert a JavaScript object into a string?

Example:

var o = {a:1, b:2}
console.log(o)
console.log(\'Item: \' + o)

Output:

30条回答
  •  [愿得一人]
    2020-11-22 04:25

    /*
        This function is as JSON.Stringify (but if you has not in your js-engine you can use this)
        Params:
            obj - your object
            inc_ident - can be " " or "\t".
            show_types - show types of object or not
            ident - need for recoursion but you can not set this parameter.
    */
    function getAsText(obj, inc_ident, show_types, ident) {
        var res = "";
        if (!ident)
            ident = "";
        if (typeof(obj) == "string") {
            res += "\"" + obj + "\" ";
            res += (show_types == true) ? "/* typeobj: " + typeof(obj) + "*/" : "";
        } else if (typeof(obj) == "number" || typeof(obj) == "boolean") {
            res += obj;
            res += (show_types == true) ? "/* typeobj: " + typeof(obj) + "*/" : "";
        } else if (obj instanceof Array) {
            res += "[ ";
            res += show_types ? "/* typeobj: " + typeof(obj) + "*/" : "";
            res += "\r\n";
            var new_ident = ident + inc_ident;
            var arr = [];
            for(var key in obj) {
                arr.push(new_ident + getAsText(obj[key], inc_ident, show_types, new_ident));
            } 
            res += arr.join(",\r\n") + "\r\n";
            res += ident + "]";
        } else {
            var new_ident = ident + inc_ident;      
            res += "{ ";
            res += (show_types == true) ? "/* typeobj: " + typeof(obj) + "*/" : "";
            res += "\r\n";
            var arr = [];
            for(var key in obj) {
                arr.push(new_ident + '"' + key + "\" : " + getAsText(obj[key], inc_ident, show_types, new_ident));
            }
            res += arr.join(",\r\n") + "\r\n";
            res += ident + "}\r\n";
        } 
        return res;
    };
    

    example to use:

    var obj = {
        str : "hello",
        arr : ["1", "2", "3", 4],
    b : true,
        vobj : {
            str : "hello2"
        }
    }
    
    var ForReading = 1, ForWriting = 2;
    var fso = new ActiveXObject("Scripting.FileSystemObject")
    f1 = fso.OpenTextFile("your_object1.txt", ForWriting, true)
    f1.Write(getAsText(obj, "\t"));
    f1.Close();
    
    f2 = fso.OpenTextFile("your_object2.txt", ForWriting, true)
    f2.Write(getAsText(obj, "\t", true));
    f2.Close();
    

    your_object1.txt:

    { 
        "str" : "hello" ,
        "arr" : [ 
            "1" ,
            "2" ,
            "3" ,
            4
        ],
        "b" : true,
        "vobj" : { 
            "str" : "hello2" 
        }
    
    }
    

    your_object2.txt:

    { /* typeobj: object*/
        "str" : "hello" /* typeobj: string*/,
        "arr" : [ /* typeobj: object*/
            "1" /* typeobj: string*/,
            "2" /* typeobj: string*/,
            "3" /* typeobj: string*/,
            4/* typeobj: number*/
        ],
        "b" : true/* typeobj: boolean*/,
        "vobj" : { /* typeobj: object*/
            "str" : "hello2" /* typeobj: string*/
        }
    
    }
    

提交回复
热议问题