JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成。它基于JavaScript的一些简单语法,用于表示复杂数据结构和序列化数据。
## JSON的语法规则
一个JSON的对象包含在花括号中,以逗号分隔的名称或值对组成。一般来说,在编写JSON对象时,应该按照以下规则:
1. 数据由键/值对组成,每个键/值对之间用逗号分隔;
```
{"name": "Tom", "age": 18}
```
2. 数据使用花括号‘{}’包裹;
```
{
"name": "Tom",
"age": 18
}
```
3. 属性名和属性值之间使用“:”分隔;
```
{"name": "Tom"}
```
4. 多个属性之间使用逗号分隔;
```
{
"name": "Tom",
"age": 18,
"gender": "male"
}
```
5. 数组使用方括号“[]”包裹,用逗号分隔数组元素;
```
{
"students": [
{
"name": "Tom",
"age": 18,
"gender": "male"
},
{
"name": "Lucy",
"age": 20,
"gender": "female"
}
]
}
```
## JSON的使用方法
### 解析JSON
在JavaScript中解析JSON,可以使用`JSON.parse()`方法将JSON字符串转换为JavaScript对象。
```
var jsonStr = '{"name":"Tom", "age":18}';
var jsonObj = JSON.parse(jsonStr);
console.log(jsonObj.name); // Tom
console.log(jsonObj.age); // 18
```
### 创建JSON
在JavaScript中可以使用`JSON.stringify()`方法将一个JS对象序列化为JSON字符串。
```javascript
var person = {
name: 'Tom',
age: 18,
gender: 'male'
};
var jsonStr = JSON.stringify(person);
console.log(jsonStr); // {"name":"Tom","age":18,"gender":"male"}
```
### 发送JSON
在HTTP请求中发送JSON数据,可以使用`XMLHttpRequest()`对象。
```javascript
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
}
}
var jsonStr = '{"name":"Tom", "age":18}';
xhr.send(jsonStr);
```
## JSON的应用案例
### 地图数据
```json
{
"markers":[
{
"name":"Shanghai, China",
"lat":"31.230416",
"lng":"121.473701"
},
{
"name":"Chongqing, China",
"lat":"29.431586",
"lng":"106.912251"
},
{
"name":"Rio de Janeiro, Brazil",
"lat":"-22.906847",
"lng":"-43.172896"
}
]
}
```
### 新闻数据
```json
{
"news":[
{
"title":"China's annual economic output to exceed $13t in 2017",
"date":"2016-03-08",
"source":"Xinhua"
},
{
"title":"China's consumer price index rose 2.3% year on year in February",
"date":"2016-03-09",
"source":"Xinhua"
},
{
"title":"China's forex reserves fall to $3.2t in February",
"date":"2016-03-11",
"source":"Xinhua"
}
]
}
```
### 股票数据
```json
[
{
"symbol": "AAPL",
"name": "Apple Inc.",
"price": "116.64",
"change": "-0.16",
"change_percent": "-0.14"
},
{
"symbol": "GOOG",
"name": "Alphabet Inc.",
"price": "751.6100",
"change": "+1.9700",
"change_percent": "+0.26%"
},
{
"symbol": "AMZN",
"name": "Amazon.com Inc.",
"price": "759.43",
"change": "+2.50",
"change_percent": "+0.33%"
}
]
```
### 天气数据
```json
{
"city":{
"id":2643743,
"name":"London",
"coord":{
"lon":-0.12574,
"lat":51.50853
},
"country":"GB",
"population":0
},
"cod":"200",
"message":0.0055,
"cnt":10,
"list":[
{"dt":1485789600,
"temp":{
"day":286.67,
"min":283.99,
"max":286.89,
"night":283.99,
"eve":285.93,
"morn":286.67},
"pressure":1019.76,
"humidity":76,
"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],
"speed":3.82,
"deg":247,
"clouds":0
},
{"dt":1485876000,
"temp":{
"day":286.08,
"min":284.64,
"max":286.68,
"night":284.64,
"eve":285.98,
"morn":286.68},
"pressure":1027.35,
"humidity":0,
"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],
"speed":1.56,
"deg":247,
"clouds":0
}
]
}
```
综上所述,JSON格式是一种轻量级的数据交换格式,使用较为广泛。通过以上的介绍,相信读者对于JSON格式有了更加深入的了解,同时也能更加熟练地使用JSON进行数据的解析和创建。 如果你喜欢我们三七知识分享网站的文章, 欢迎您分享或收藏知识分享网站文章 欢迎您到我们的网站逛逛喔!https://www.37seo.cn/
幸福永远与爱同在。