<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <script type ="text/javascript">
        var cmdType = "ChangeDisplayName";
        var json = { "a1": "a111", "b1": "b111" };        var tmp = { cmdType: json };
       //这里弹出的是:{"cmdType":{"a1":"a111","b1":"b111"}}
       //问题是希望弹出的是:{"ChangeDisplayName":{"a1":"a111","b1":"b111"}}
        alert(json_encode_js(tmp)); 
        
        //这只是个生成JSON的辅助函数,跟这个问题无关
        function json_encode_js(aaa) {
            function je(str) {
                var a = [], i = 0;
                var pcs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                for (; i < str.length; i++) {
                    if (pcs.indexOf(str[i]) == -1)
                        a[i] = "\\u" + ("0000" + str.charCodeAt(i).toString(16)).slice(-4);
                    else
                        a[i] = str[i];
                }
                return a.join("");
            }
            var i, s, a, aa = [];
            if (typeof (aaa) != "object") { alert("ERROR json"); return; }
            for (i in aaa) {
                s = aaa[i];
                a = '"' + je(i) + '":';
                if (typeof (s) == 'object') {
                    a += json_encode_js(s);
                } else {
                    if (typeof (s) == 'string')
                        a += '"' + je(s) + '"';
                    else if (typeof (s) == 'number')
                        a += s;
                }
                aa[aa.length] = a;
            }
            return "{" + aa.join(",") + "}";
        }
    </script>
</head>
<body></body>
</html>问题:
       //这里弹出的是:{"cmdType":{"a1":"a111","b1":"b111"}}
       //问题是希望弹出的是:{"ChangeDisplayName":{"a1":"a111","b1":"b111"}}
        alert(json_encode_js(tmp)); 我就是想生成这样一个json字符串:{"ChangeDisplayName":{"a1":"a111","b1":"b111"}},因为“ChangeDisplayName”这部分是在程序运行时动态生成的,所以不能写死,但是写成变量有遇到上面的问题,请问有没有什么好办法?

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Untitled Page</title>
        <script type ="text/javascript">
            var cmdType = "ChangeDisplayName";
            var json = '{ "a1": "a111", "b1": "b111" }';        var tmp = "{ '" + cmdType + "': " + json +" }";
           //这里弹出的是:{"cmdType":{"a1":"a111","b1":"b111"}}
           //问题是希望弹出的是:{"ChangeDisplayName":{"a1":"a111","b1":"b111"}}
            var temp = eval("("+tmp+")") 
            for(var i in temp) {
    alert(i);
    for(var j in temp[i]) {
    alert(j + ":" +temp[i][j]);
    }
    }
            //这只是个生成JSON的辅助函数,跟这个问题无关
            function json_encode_js(aaa) {
                function je(str) {
                    var a = [], i = 0;
                    var pcs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                    for (; i < str.length; i++) {
                        if (pcs.indexOf(str[i]) == -1)
                            a[i] = "\\u" + ("0000" + str.charCodeAt(i).toString(16)).slice(-4);
                        else
                            a[i] = str[i];
                    }
                    return a.join("");
                }
                var i, s, a, aa = [];
                if (typeof (aaa) != "object") { alert("ERROR json"); return; }
                for (i in aaa) {
                    s = aaa[i];
                    a = '"' + je(i) + '":';
                    if (typeof (s) == 'object') {
                        a += json_encode_js(s);
                    } else {
                        if (typeof (s) == 'string')
                            a += '"' + je(s) + '"';
                        else if (typeof (s) == 'number')
                            a += s;
                    }
                    aa[aa.length] = a;
                }
                return "{" + aa.join(",") + "}";
            }
        </script>
    </head>
    <body></body>
    </html>
      

  2.   

    to cj205:
        非常感谢,能说说其中道理吗?
      

  3.   

            似乎这样就行了:
          var tmp = "{ '" + cmdType + "': " + json +" }";
    alert(tmp);//输出{ 'ChangeDisplayName': { "a1": "a111", "b1": "b111" } }