how2j.cn


工具版本兼容问题
JSON JavaScript 对象表示法(JavaScript Object Notation) 是一种存储数据的方式。

示例 1 : 创建JSON对象   
示例 2 : 访问JSON对象   

示例 1 :

创建JSON对象

var gareen = {"name":"盖伦","hp":616};
这样就创建了一个JSON 对象
JSON对象由 名称/值对组成 名称和值之间用冒号:隔开
名称必须用双引号" 包含起来
值可以是任意javascript数据类型,字符串,布尔,数字 ,数组甚至是对象
不同的名称/值对之间用 逗号 , 隔开
"); window.frames["iframe_show1237"].document.write(decodeHtml($("textarea#stepcodeTextarea1237").val())); window.frames["iframe_show1237"].document.close(); $(window.frames["iframe_show1237"]).load(function(){ $("#iframe_show1237").height($("#iframe_show1237").contents().find("body").height()+showittryitheight); }); $("#iframe_show1237").height($("#iframe_show1237").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var gareen = {"name":"盖伦","hp":616}; document.write("这是一个JSON对象: "+gareen); </script>
<script>
var gareen = {"name":"盖伦","hp":616};
 
document.write("这是一个JSON对象: "+gareen);

</script>
"); window.frames["iframe1237"].document.write(decodeHtml(code1237)); window.frames["iframe1237"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1237"]).load(function(){ $("#iframe1237").height($("#iframe1237").contents().find("body").height()+showittryitheight); }); $("#iframe1237").height($("#iframe1237").contents().find("body").height()+showittryitheight); alreadyWriteCode1237 = code1237; $("#rendering1237").hide(); $("#rendered1237").show(); } var tRereshRetry2DemoPanel1237 = setInterval(rereshRetry2DemoPanel1237,1000); var binded1237 = false; $("textarea#stepcodeTextarea1237").keyup(function(){ if(!binded1237){ $(window).bind('beforeunload',function(){ binded1237 = true; return "xxxx"; }); } var newCode = $(this).val() code1237 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1237!=newCode){ // window.frames["iframe1237"].document.write("
"); // window.frames["iframe1237"].document.write(decodeHtml($("textarea#stepcodeTextarea1237").val())); // window.frames["iframe1237"].document.close(); // $(window.frames["iframe1237"]).load(function(){ // $("#iframe1237").height($("#iframe1237").contents().find("body").height()+showittryitheight); // }); // code1237 = newCode; // } }); $(".tryButton1237").click(function(){ $("#tryDiv1237").show(); $("#stepcodeTextarea1237").focus(); $("#stepcodeTextarea1237").height(200); $("#iframe1237").height(0); window.frames["iframe1237"].document.write("
"); window.frames["iframe1237"].document.write(decodeHtml($("textarea#stepcodeTextarea1237").val())); window.frames["iframe1237"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1237"]).load(function(){ $("#iframe1237").height($("#iframe1237").contents().find("body").height()+showittryitheight); }); $("#iframe1237").height($("#iframe1237").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1237.focus(); editor1237.setSize(null, "250"); $("#rendering1237").hide(); $("#rendered1237").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 editor1237 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1237"), { 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); } } }); editor1237.on("change",function(doc){ if(!binded1237){ $(window).bind('beforeunload',function(){ binded1237 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1237 = newCode; $("textarea#stepcodeTextarea1237").val(newCode); if(alreadyWriteCode1237!=code1237){ lastModifedTime1237 = new Date().getTime(); $("#rendering1237").show(); $("#rendered1237").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1237 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1237.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1237").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

访问JSON对象

通过 点. 访问JSON对象的属性
"); window.frames["iframe_show1238"].document.write(decodeHtml($("textarea#stepcodeTextarea1238").val())); window.frames["iframe_show1238"].document.close(); $(window.frames["iframe_show1238"]).load(function(){ $("#iframe_show1238").height($("#iframe_show1238").contents().find("body").height()+showittryitheight); }); $("#iframe_show1238").height($("#iframe_show1238").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var gareen = {"name":"盖伦","hp":616}; document.write("英雄名称: " + gareen.name + "<br>"); document.write("英雄血量: " + gareen.hp + "<br>"); </script>
<script>
var gareen = {"name":"盖伦","hp":616};

document.write("英雄名称: " + gareen.name + "<br>");
document.write("英雄血量: " + gareen.hp + "<br>");
</script>
"); window.frames["iframe1238"].document.write(decodeHtml(code1238)); window.frames["iframe1238"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1238"]).load(function(){ $("#iframe1238").height($("#iframe1238").contents().find("body").height()+showittryitheight); }); $("#iframe1238").height($("#iframe1238").contents().find("body").height()+showittryitheight); alreadyWriteCode1238 = code1238; $("#rendering1238").hide(); $("#rendered1238").show(); } var tRereshRetry2DemoPanel1238 = setInterval(rereshRetry2DemoPanel1238,1000); var binded1238 = false; $("textarea#stepcodeTextarea1238").keyup(function(){ if(!binded1238){ $(window).bind('beforeunload',function(){ binded1238 = true; return "xxxx"; }); } var newCode = $(this).val() code1238 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1238!=newCode){ // window.frames["iframe1238"].document.write("
"); // window.frames["iframe1238"].document.write(decodeHtml($("textarea#stepcodeTextarea1238").val())); // window.frames["iframe1238"].document.close(); // $(window.frames["iframe1238"]).load(function(){ // $("#iframe1238").height($("#iframe1238").contents().find("body").height()+showittryitheight); // }); // code1238 = newCode; // } }); $(".tryButton1238").click(function(){ $("#tryDiv1238").show(); $("#stepcodeTextarea1238").focus(); $("#stepcodeTextarea1238").height(200); $("#iframe1238").height(0); window.frames["iframe1238"].document.write("
"); window.frames["iframe1238"].document.write(decodeHtml($("textarea#stepcodeTextarea1238").val())); window.frames["iframe1238"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1238"]).load(function(){ $("#iframe1238").height($("#iframe1238").contents().find("body").height()+showittryitheight); }); $("#iframe1238").height($("#iframe1238").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1238.focus(); editor1238.setSize(null, "250"); $("#rendering1238").hide(); $("#rendered1238").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 editor1238 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1238"), { 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); } } }); editor1238.on("change",function(doc){ if(!binded1238){ $(window).bind('beforeunload',function(){ binded1238 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1238 = newCode; $("textarea#stepcodeTextarea1238").val(newCode); if(alreadyWriteCode1238!=code1238){ lastModifedTime1238 = new Date().getTime(); $("#rendering1238").show(); $("#rendered1238").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1238 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1238.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1238").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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


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


关于 前端部分-JSON-Hello JSON 的提问

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

上传截图