In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
今天就跟大家聊聊有关JavaScript json解析是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
JSON的一个常见用途是交换数据到从一个Web服务器,当从web服务器接收数据时,数据总是一个字符串。使用JSON.parse()方法解析这些数据,数据变成JavaScript对象。
一、浏览器支持
二、实例 - 解析JSON
1. 页面解析
想象一下,我们从Web服务器收到这个文本:
'{ "name":"John", "age":30, "city":"New York"}'
使用JavaScript函数JSON.parse() 将文本转换成JavaScript对象:
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');
注:确保文字是用JSON格式,否则你会得到一个语法错误。
在你的页面中使用JavaScript对象:
项目 JSON解析文本
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}'); document.getElementById("demo")[xss_clean] = obj.name + ", " + obj.age;
2. 从服务器获得JSON
你可以使用AJAX请求从服务器获得JSON。
只要来自服务器的响应是用JSON格式,您可以将字符串解析成JavaScript对象。
Ajax是什么?
Asynchronous JavaScript & XML。Ajax是web开发的一种技术。
var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myObj = JSON.parse(this.responseText); document.getElementById("demo")[xss_clean] = myObj.name; } }; xmlhttp.open("GET", "json_demo.txt", true); xmlhttp.send();
服务器端 访问成功!将JSON解析的内容保存在json_demo.txt。
3. 数组作为JSON
当使用JSON.parse()方法的使用, 该方法将返回一个JavaScript数组, 而不是一个JavaScript对象.
var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myArr = JSON.parse(this.responseText); document.getElementById("demo")[xss_clean] = myArr[0]; } }; xmlhttp.open("GET", "json_demo_array.txt", true); xmlhttp.send();
为了方便测速,自己创建了json_demo_array.txt文件。
json_demjo_array.txt
三、拓展
解析日期(2种)
在JSON中,日期对象是不允许的。
如果需要包含日期,请将其写成字符串.
1. 将其转换为日期对象:
var text = '{ "name":"John", "birth":"1986-12-14", "city":"New York"}'; var obj = JSON.parse(text) { obj.birth = new Date(obj.birth); document.getElementById("demo")[xss_clean] = obj.name + ", " + obj.birth;
2.使用JSON.parse()函数的第二个参数, 称为接收器
该接收机参数,返回值之前,检查每个属性。
var text = '{ "name":"John", "birth":"1986-12-14", "city":"New York"}'; var obj = JSON.parse(text, function (key, value) { if (key == "birth") { return new Date(value); } else { return value; }}); document.getElementById("demo")[xss_clean] = obj.name + ", " + obj.birth;
解析函数
在JSON中不允许函数.
如果你需要包含一个函数,写成字符串。
以后您可以将其转换为函数:
var text = '{ "name":"John", "age":"function () {return 30;}", "city":"New York"}'; var obj = JSON.parse(text) { obj.age = eval("(" + obj.age + ")"); document.getElementById("demo")[xss_clean] = obj.name + ", " + obj.age();
Avoid using functions in JSON, functions will lose their scope and you will have to use eval() to convert them back into functions.
After reading the above, do you know more about JavaScript json parsing? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.