<!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”这部分是在程序运行时动态生成的,所以不能写死,但是写成变量有遇到上面的问题,请问有没有什么好办法?
解决方案 »
- 为什么 window.open()指定在某个iframe打开,只能执行一次
- javascript函数方面的问题
- javascript跨域问题
- 请问如何在innerHTML里面输入一段js脚本?
- 再次询问YUI的问题,谢谢
- javascript验证 动态单选框
- extjs renderer传值
- 一个赋值问题!!
- 如何生成一个自定义大小的浏览框......以及.....?急急急!!!!
- 我在一个table里做了一个树形菜单,但是当菜单项全部打开时table的height就会自动加长,如何使height保持不变?
- 请个高手哥哥帮我把一小段php代码转换成js
- extjs, combobox,在Onblur事件中,如何,得到刚才输入的数据,急
<!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>
非常感谢,能说说其中道理吗?
var tmp = "{ '" + cmdType + "': " + json +" }";
alert(tmp);//输出{ 'ChangeDisplayName': { "a1": "a111", "b1": "b111" } }