json basic + instance

Brothers, I only talk about the core
Just to modify and delete
//Modification

myobj.ke[0]= "HTML Course"
        var i,x="";
		for(i in myobj.ke)
		{
			x = x + i + " | " + myobj.ke[i] + "<br>"
		}
		document.getElementById("demo02").innerHTML = x;

The core is coverage
Other students who don't understand please look at 123 ha, you really can dance, brother dei

//Delete

delete myobj.ke[2]
		var j,y = "";
		for(j=0;j<myobj.ke.length;j++)
		{
			y = y + myobj.ke[j] + "<br>"
		}
		document.getElementById("demo03").innerHTML = y;
	The core is to delete the delete myobj.ke[2], because no, the browser thinks you have undefined it, brother dei

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>

</head>

<body>

    <h2>JSON array04-Array in object-Modification and deletion</h2>
    
	<div id="demo01"></div><hr>
    <div id="demo02"></div><hr>
    <div id="demo03"></div>
 
    
    <script type="text/javascript">
 		var myobj;
		myobj={
			"xm":"Huang Juhua",
			"num":3,
			"ke":["html","css","js"]
			}
		document.getElementById("demo01").innerHTML =myobj.ke[0]+"<br>";
		document.getElementById("demo01").innerHTML = document.getElementById("demo01").innerHTML + myobj.ke[1]+"<br>";
		document.getElementById("demo01").innerHTML = document.getElementById("demo01").innerHTML + myobj.ke[2]+"<br>";

		//modify
		myobj.ke[0]= "HTML Course"
        var i,x="";
		for(i in myobj.ke)
		{
			x = x + i + " | " + myobj.ke[i] + "<br>"
		}
		document.getElementById("demo02").innerHTML = x;
		
		//delete
		delete myobj.ke[2]
		var j,y = "";
		for(j=0;j<myobj.ke.length;j++)
		{
			y = y + myobj.ke[j] + "<br>"
		}
		document.getElementById("demo03").innerHTML = y;
		
		
		
	</script>


</body>
</html>

Brothers, here comes the core, super nesting
!!!!
Remember, brothers, the object is the attribute, the array is the subscript starting from 0, core ha·
For example, in this case, the attribute in the object is name num sites
The array subscript in the sites property is
1 {"name": "Taobao", "info": ["shopping website", "www.taobao.com", "e-commerce"]},
2 {"name": "QQ", "info": ["comprehensive website", "www.qq.com", "comprehensive"]},
3 {"name": "Baidu", "info": ["search website", "www.baidu.com", "search"]}
Remember, as I said before, you can write arrays in attributes
Then the attribute in the object is name info
And then the properties in the object are arrays
0 ["shopping website", "www.taobao.com", "e-commerce"]
1 ["comprehensive website", "www.qq.com", "comprehensive"]
2 ["search website", "www.baidu.com", "search"]
It's that simple. You know













obj = {
			"name":"website",
			"num":3,
			"sites":[
				{"name":"TaoBao","info":["Shopping website","www.taobao.com","Online retailers"]},
				{"name":"QQ","info":["Comprehensive website","www.qq.com","comprehensive"]},
				{"name":"Baidu","info":["Search website","www.baidu.com","search"]}
			]
		}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>

</head>

<body>

    <h2>1JSON array05-Complex nesting</h2>
    
	<div id="demo01"></div>
 
 
    
    <script type="text/javascript">
 		var obj,i,j,x ="";
		obj = {
			"name":"website",
			"num":3,
			"sites":[
				{"name":"TaoBao","info":["Shopping website","www.taobao.com","Online retailers"]},
				{"name":"QQ","info":["Comprehensive website","www.qq.com","comprehensive"]},
				{"name":"Baidu","info":["Search website","www.baidu.com","search"]}
			]
		}
		
		for(i in obj.sites)//There are three arrays traversing the sites property
		{
			x = x + "<h1>" + obj.sites[i].name + "</h1>"//The three time
			//Then the value of the first () subscript 0) name is:
			for(j in obj.sites[i].info)//Then we got "info": ["shopping website", "www.taobao.com", "e-commerce"] three times
			{
				x = x + obj.sites[i].info[j] + "<br>";//Then we need to go through {"name": "Taobao", "info": ["shopping website", "www.taobao.com", "e-commerce"]} nine times in total
				//Inside ["shopping website", "www.taobao.com", "e-commerce"], I'll follow my thinking
			}
		}
		
		document.getElementById("demo01").innerHTML =x;
		
	</script>


</body>
</html>


OK, we want to understand, bye!!!!!

Tags: Attribute Javascript JSON

Posted on Tue, 05 May 2020 08:17:48 -0700 by oskom