how2j.cn

示例 1 : JSON对象与JavaScript对象   
示例 2 : 字符串转为JSON对象   
示例 3 : JSON 对象转换为字符串   

示例 1 :

JSON对象与JavaScript对象

JavaScript对象 分内置对象(Number,String,Array,Date,Math)和自定义对象
JSON就是自定义对象,只不过是以JSON这样的数据组织方式表达出来
所以不存在JSON对象与JavaScript对象的转换问题
示例 2 :

字符串转为JSON对象

通过字符串拼接得到一个JSON结构的字符串,并不是一个JSON对象。 需要通过eval转换得到
转换的时候注意,eval 函数要以( 开头,)结尾
或者使用JQuery的$.parseJSON转换函数
"); window.frames["iframe_show1242"].document.write(decodeHtml($("textarea#stepcodeTextarea1242").val())); window.frames["iframe_show1242"].document.close(); $(window.frames["iframe_show1242"]).load(function(){ $("#iframe_show1242").height($("#iframe_show1242").contents().find("body").height()+showittryitheight); }); $("#iframe_show1242").height($("#iframe_show1242").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var s1 = "{\"name\":\"盖伦\""; var s2 = ",\"hp\":616}"; var s3 = s1+s2; document.write("这是一个JSON格式的字符串:" + s3); document.write("<br>"); var gareen = eval("("+s3+")"); document.write("这是一个JSON对象: " + gareen); </script>
<script>

var s1 = "{\"name\":\"盖伦\"";
var s2 = ",\"hp\":616}";
var s3 = s1+s2;

document.write("这是一个JSON格式的字符串:" + s3);
document.write("<br>");
var gareen = eval("("+s3+")");

document.write("这是一个JSON对象: " + gareen);
 
</script>
"); window.frames["iframe1242"].document.write(decodeHtml(code1242)); window.frames["iframe1242"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1242"]).load(function(){ $("#iframe1242").height($("#iframe1242").contents().find("body").height()+showittryitheight); }); $("#iframe1242").height($("#iframe1242").contents().find("body").height()+showittryitheight); alreadyWriteCode1242 = code1242; $("#rendering1242").hide(); $("#rendered1242").show(); } var tRereshRetry2DemoPanel1242 = setInterval(rereshRetry2DemoPanel1242,1000); var binded1242 = false; $("textarea#stepcodeTextarea1242").keyup(function(){ if(!binded1242){ $(window).bind('beforeunload',function(){ binded1242 = true; return "xxxx"; }); } var newCode = $(this).val() code1242 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1242!=newCode){ // window.frames["iframe1242"].document.write("
"); // window.frames["iframe1242"].document.write(decodeHtml($("textarea#stepcodeTextarea1242").val())); // window.frames["iframe1242"].document.close(); // $(window.frames["iframe1242"]).load(function(){ // $("#iframe1242").height($("#iframe1242").contents().find("body").height()+showittryitheight); // }); // code1242 = newCode; // } }); $(".tryButton1242").click(function(){ $("#tryDiv1242").show(); $("#stepcodeTextarea1242").focus(); $("#stepcodeTextarea1242").height(200); $("#iframe1242").height(0); window.frames["iframe1242"].document.write("
"); window.frames["iframe1242"].document.write(decodeHtml($("textarea#stepcodeTextarea1242").val())); window.frames["iframe1242"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1242"]).load(function(){ $("#iframe1242").height($("#iframe1242").contents().find("body").height()+showittryitheight); }); $("#iframe1242").height($("#iframe1242").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1242.focus(); editor1242.setSize(null, "250"); $("#rendering1242").hide(); $("#rendered1242").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1242 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1242"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1242.on("change",function(doc){ if(!binded1242){ $(window).bind('beforeunload',function(){ binded1242 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1242 = newCode; $("textarea#stepcodeTextarea1242").val(newCode); if(alreadyWriteCode1242!=code1242){ lastModifedTime1242 = new Date().getTime(); $("#rendering1242").show(); $("#rendered1242").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1242 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1242.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1242").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 3 :

JSON 对象转换为字符串

json 对象因为是一个javascript对象,所以如果直接打印的话,看不到里面的内容。
有时候要看看这个对象是不是我们期望的,所以需要通过 JSON.stringify 函数把它转换为 字符串
"); window.frames["iframe_show7833"].document.write(decodeHtml($("textarea#stepcodeTextarea7833").val())); window.frames["iframe_show7833"].document.close(); $(window.frames["iframe_show7833"]).load(function(){ $("#iframe_show7833").height($("#iframe_show7833").contents().find("body").height()+showittryitheight); }); $("#iframe_show7833").height($("#iframe_show7833").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var hero = {"name":"盖伦","hp":"616"}; document.write("这是一个json 对象:"+ hero); document.write("<br>"); var heroString = JSON.stringify(hero) document.write("这是一个json 字符串:"+ heroString ); </script>
<script>
var hero = {"name":"盖伦","hp":"616"};
document.write("这是一个json 对象:"+ hero);
document.write("<br>");
var heroString = JSON.stringify(hero)
document.write("这是一个json 字符串:"+ heroString );
</script>
"); window.frames["iframe7833"].document.write(decodeHtml(code7833)); window.frames["iframe7833"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7833"]).load(function(){ $("#iframe7833").height($("#iframe7833").contents().find("body").height()+showittryitheight); }); $("#iframe7833").height($("#iframe7833").contents().find("body").height()+showittryitheight); alreadyWriteCode7833 = code7833; $("#rendering7833").hide(); $("#rendered7833").show(); } var tRereshRetry2DemoPanel7833 = setInterval(rereshRetry2DemoPanel7833,1000); var binded7833 = false; $("textarea#stepcodeTextarea7833").keyup(function(){ if(!binded7833){ $(window).bind('beforeunload',function(){ binded7833 = true; return "xxxx"; }); } var newCode = $(this).val() code7833 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7833!=newCode){ // window.frames["iframe7833"].document.write("
"); // window.frames["iframe7833"].document.write(decodeHtml($("textarea#stepcodeTextarea7833").val())); // window.frames["iframe7833"].document.close(); // $(window.frames["iframe7833"]).load(function(){ // $("#iframe7833").height($("#iframe7833").contents().find("body").height()+showittryitheight); // }); // code7833 = newCode; // } }); $(".tryButton7833").click(function(){ $("#tryDiv7833").show(); $("#stepcodeTextarea7833").focus(); $("#stepcodeTextarea7833").height(200); $("#iframe7833").height(0); window.frames["iframe7833"].document.write("
"); window.frames["iframe7833"].document.write(decodeHtml($("textarea#stepcodeTextarea7833").val())); window.frames["iframe7833"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7833"]).load(function(){ $("#iframe7833").height($("#iframe7833").contents().find("body").height()+showittryitheight); }); $("#iframe7833").height($("#iframe7833").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7833.focus(); editor7833.setSize(null, "250"); $("#rendering7833").hide(); $("#rendered7833").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor7833 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7833"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor7833.on("change",function(doc){ if(!binded7833){ $(window).bind('beforeunload',function(){ binded7833 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7833 = newCode; $("textarea#stepcodeTextarea7833").val(newCode); if(alreadyWriteCode7833!=code7833){ lastModifedTime7833 = new Date().getTime(); $("#rendering7833").show(); $("#rendered7833").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7833 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7833.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7833").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-JSON-对象转换 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图