May 08, 2021 JSON
A simple JSON object syntax:
{ "name":"w3cschool", "alexa":8000, "site":null }
JSON objects are written in braces, and objects can contain multiple key/value (key/value) pairs.
Among them:
JSON objects can be created using JavaScript. Let's look at the various ways to create JSON objects using JavaScript:
var JSONObj = {};
var JSONObj = new Object();
var JSONObj = { "bookname ":"VB BLACK BOOK", "price":500 };
Here's an example of creating an object with JSON in JavaScript, and you can save the following code as json_object_w3cschool.htm:
<html>
<head>
<title>在JavaScript中使用JSON创建对象(w3cschool.cn)</title>
<script language="javascript" >
var JSONObj = { "name" : "编程狮(w3cschool.cn)", "time" : 2020 };
document.write("<h1>JSON和JavaScript的例子</h1>");
document.write("<h3>网站的名字="+JSONObj.name+"</h3>");
document.write("<h3>时间="+JSONObj.time+"</h3>");
</script>
</head>
<body>
</body>
</html>
Now let's try opening this page using IE or any other JavaScript-enabled browser, which will produce the following results:
You can use the dot number (.) to access the value of the object:
You can also use parentheses to access the value of an object:
You can use for-in to loop the properties of an object:
When you loop an object's property for-in, use parentheses to access the value of the property:
The following example shows an array object created with JSON in JavaScript, and you can save the following code as json_array_object_w3cschool.htm:
<html>
<head>
<title>在 JavaScript 中使用 JSON 创建数组对象(w3cschool.cn)</title>
<script language="javascript" >
document.writeln("<h2>JSON数组对象(w3cschool.cn)</h2>");
var books = {
"Pascal" : [
{ "Name" : "Pascal Made Simple", "price" : 700 },
{ "Name" : "Guide to Pascal", "price" : 400 }
],
"Scala" : [
{ "Name" : "Scala for the Impatient", "price" : 1000 },
{ "Name" : "Scala in Depth", "price" : 1300 }
]
}
var i = 0
document.writeln("<table border='2'><tr>");
for(i=0;i<books.Pascal.length;i++)
{
document.writeln("<td>");
document.writeln("<table border='1' width=100 >");
document.writeln("<tr><td><b>Name</b></td><td width=50>"
+ books.Pascal[i].Name+"</td></tr>");
document.writeln("<tr><td><b>Price</b></td><td width=50>"
+ books.Pascal[i].price +"</td></tr>");
document.writeln("</table>");
document.writeln("</td>");
}
for(i=0;i<books.Scala.length;i++)
{
document.writeln("<td>");
document.writeln("<table border='1' width=100 >");
document.writeln("<tr><td><b>Name</b></td><td width=50>"
+ books.Scala[i].Name+"</td></tr>");
document.writeln("<tr><td><b>Price</b></td><td width=50>"
+ books.Scala[i].price+"</td></tr>");
document.writeln("</table>");
document.writeln("</td>");
}
document.writeln("</tr></table>");
</script>
</head>
<body>
</body>
</html>
Now let's try opening this page using IE or any other JavaScript-enabled browser, and it will produce results like this:
The JSON object can contain another JSON object:
myObj = {
"name":"w3cschool",
"alexa":10000,
"sites": {
"site1":"www.w3cschool.cn",
"site2":"m.w3cschool.cn",
"site3":"c.w3cschool.cn"
}
}
You can use the dot number (.) Or parentheses to access nested JSON objects.
You can use the dot number (.) to modify the value of the JSON object:
You can modify the value of the JSON object using parentheses:
The difference between a JSON object and a JSON string:
JSON object
var str2 = { "name": "asan", "sex": "man" };
JSON string
var str1 = '{ "name": "deyuyi", "sex": "man" }';