【json】json基礎知識

Alearn_發表於2018-06-21

json語法詳見j菜鳥教程

關於json的一些基礎知識儲備

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
</head>
<body>
<h2>JavaScript 建立 JSON 物件</h2>
<p>
網站名稱: <span id="jname"></span><br /> 
網站地址: <span id="jurl"></span><br /> 
網站 slogan: <span id="jslogan"></span><br /> 
</p>
<div id="demo"></div>
<div id="demo1"></div>
<script>
var JSONObject= {
    "name":"菜鳥教程",
    "url":"www.noob.com", 
    "slogan":"學的不僅是技術,更是夢想!"
};
    var obj={
        "sites": [
            {"name":"菜鳥教程aa","url":"###","slogan":null},
            {"name":"菜鳥教程bb","url":"###","slogan":null}
        ]
    }
    var x = obj.sites[0].name;
    var y = "";
    for (i in obj.sites) {
        y += obj.sites[i].name + "<br>";
    }
document.getElementById("jname").innerHTML=JSONObject.name 
document.getElementById("jurl").innerHTML=JSONObject.url 
document.getElementById("jslogan").innerHTML=JSONObject.slogan 
document.getElementById("demo").innerHTML = x;
document.getElementById("demo1").innerHTML = y;

    </script>

</body>
</html>