一、从JSON方法爬数据
在互联网上,获取数据是一项基本而必要的工作。JSON方法可以让我们轻松地采集和解析网页上的数据。
我们以爬取股票数据为例,使用JavaScript中的fetch
函数获取数据,然后使用json()
方法解析返回的JSON字符串。
fetch('https://example.com/stockdata.json') .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error));
上述代码中,fetch
函数用于获取数据,返回的是一个Promise
对象,我们通过then()
方法链式调用,使用json()
方法对返回的响应进行解析。由于json()
方法也是返回一个Promise
对象,因此也可以继续使用then()
方法对解析后的数据进行操作。如果在请求或解析过程中发生错误,可以使用catch()
方法处理错误。
二、JSON的语法规范
JSON的语法是由一些基本元素组成,包括字符串、数字、布尔值和空值等。
一个简单的JSON示例:
{ "name": "John Smith", "age": 30, "isMarried": true, "address": { "street": "Main Street", "city": "New York", "state": "NY", "zip": "10001" } }
其中,整个JSON对象需要用一对花括号{}
括起来;属性名和属性值之间需要用冒号:
分隔;不同的属性之间需要用逗号,
分隔。字符串需要用引号" "
括起来,可以使用单引号' '
代替。数字、布尔值和空值不需要引号括起来。
三、JSON方法JS
JSON方法常用于JavaScript编程中,其中JSON.parse()
方法用于将JSON字符串转换为JavaScript对象,JSON.stringify()
方法用于将JavaScript对象转换为JSON字符串。
例如:
//将JSON字符串转换为JavaScript对象 const jsonStr = '{"name": "John Smith", "age": 30}'; const jsonObj = JSON.parse(jsonStr); console.log(jsonObj.name); //"John Smith" //将JavaScript对象转换为JSON字符串 const jsonObj = {name: "John Smith", age: 30}; const jsonStr = JSON.stringify(jsonObj); console.log(jsonStr); //'{"name":"John Smith","age":30}'
四、处理JSON的方法
在处理JSON时,有一些常用的方法可以帮助我们更加有效的操作JSON数据。
1. hasOwnProperty()
hasOwnProperty()
方法用于检查一个对象是否包含特定的属性,它会返回一个布尔值。
const jsonObj = {name: "John Smith", age: 30}; console.log(jsonObj.hasOwnProperty('name')); //true console.log(jsonObj.hasOwnProperty('gender')); //false
2. Object.keys()
Object.keys()
方法返回一个对象自身属性的键(键是以数组形式返回的)。
const jsonObj = {name: "John Smith", age: 30}; const keys = Object.keys(jsonObj); console.log(keys); //["name", "age"]
3. Object.values()
Object.values()
方法返回一个对象自身属性的值(值是以数组形式返回的)。
const jsonObj = {name: "John Smith", age: 30}; const values = Object.values(jsonObj); console.log(values); //["John Smith", 30]
4. Object.entries()
Object.entries()
方法返回一个对象自身属性的键/值对(键值对是以数组形式返回的)。
const jsonObj = {name: "John Smith", age: 30}; const entries = Object.entries(jsonObj); console.log(entries); //[["name", "John Smith"], ["age", 30]]
五、JSON方法汇总
除了以上几种常用的方法之外,JSON还有很多实用的方法,如下表所示:
方法 | 描述 |
---|---|
JSON.parse() |
将JSON字符串转换为JavaScript对象 |
JSON.stringify() |
将JavaScript对象转换为JSON字符串 |
JSON.parseWithReviver() |
使用自定义函数解析JSON字符串 |
JSON.stringifyWithReplacer() |
使用自定义函数将JavaScript对象转换为JSON字符串 |
JSON.stringifyWithSpacer() |
将JavaScript对象转换为带缩进的JSON字符串 |
六、JSON方法的功能
JSON方法的功能非常广泛,可以用于数据交换、数据存储、数据传输等多个方面。
1. 数据交换
JSON方法可以方便地实现不同系统之间的数据交换。
//从服务器获取JSON数据 fetch('https://example.com/data.json') .then(response => response.json()) .then(data => process(data)) .catch(error => console.error(error)); //将数据上传到服务器 fetch('https://example.com/save', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) }) .then(response => console.log(response)) .catch(error => console.error(error));
2. 数据存储
JSON方法可以方便地将数据存储在本地。
//将数据存储在localStorage中 const data = {name: "John Smith", age: 30}; localStorage.setItem('data', JSON.stringify(data)); //从localStorage中获取数据 const dataStr = localStorage.getItem('data'); const data = JSON.parse(dataStr); console.log(data.name); //"John Smith"
3. 数据传输
JSON方法可以方便地在客户端和服务器之间传输数据。
//将数据上传到服务器 fetch('https://example.com/save', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data) }) .then(response => console.log(response)) .catch(error => console.error(error)); //从服务器获取JSON数据 fetch('https://example.com/data.json') .then(response => response.json()) .then(data => process(data)) .catch(error => console.error(error));
七、JSON方法返回值
JSON方法的返回值可以是任何类型的值,例如:
const jsonObj = {name: "John Smith", age: 30}; const jsonStr = JSON.stringify(jsonObj); console.log(jsonStr); //字符串 const jsonArr = ['John', 'Smith', 30]; const jsonObj2 = JSON.parse(jsonArr); console.log(jsonObj2); //对象 const jsonFunc = function() {console.log('Hello world')}; const jsonStr2 = JSON.stringify(jsonFunc); console.log(jsonStr2); //undefined
八、JSON方法深拷贝
JSON方法可以实现对象的深拷贝,即拷贝出一个新的完全独立的对象。
const jsonObj = {name: "John Smith", age: 30, address: {city: "New York", state: "NY"}}; const jsonObj2 = JSON.parse(JSON.stringify(jsonObj)); jsonObj2.address.city = "Los Angeles"; console.log(jsonObj.address.city); //"New York" console.log(jsonObj2.address.city); //"Los Angeles"
九、JSON方法的缺点
JSON方法也有一些不足之处,其中最明显的一个问题是性能。
由于JSON方法需要进行字符串化和解析操作,因此在处理大量数据时,性能可能会受到影响。在这种情况下,最好使用其他更高效的数据格式,例如二进制格式。
虽然JSON方法的性能问题确实存在,但在大多数应用程序中,它仍然是一个非常实用和灵活的数据格式。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/157587.html