博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSON in JavaScript
阅读量:2456 次
发布时间:2019-05-10

本文共 2027 字,大约阅读时间需要 6 分钟。

is a general purpose programming language that was introduced as the page scripting language for Netscape Navigator. . It is a -like language with and . JavaScript was standardized in the .

is a subset of the object literal notation of JavaScript. Since JSON is a subset of JavaScript, it can be used in the language with no muss or fuss.

var myJSONObject = {"bindings": [         {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},         {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},         {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}     ] };

In this example, an object is created containing a single member "bindings", which contains an array containing three objects, each containing "ircEvent", "method", and "regex" members.

Members can be retrieved using dot or subscript operators.

myJSONObject.bindings[0].method    // "newURI"

To convert a JSON text into an object, use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure.

var myObject = eval('(' + myJSONtext + ')');

The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted. This is commonly the case in web applications when a web server is providing both the base page and the JSON data. There are cases where the source is not trusted. In particular, clients should never be trusted.

When security is a concern it is better to use a JSON parser. A JSON parser will only recognize JSON text and so is much safer:

var myObject = myJSONtext.parseJSON();

A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text. JSON does not support cyclic data structures, so be careful to not give cyclical structures to the JSON stringifier.

var myJSONText = myObject.toJSONString();

When it is only about 1.5K.

 

转载地址:http://vsjhb.baihongyu.com/

你可能感兴趣的文章
zeppelin连接数据源_使用开放源代码合同(open-zeppelin)创建以太坊令牌
查看>>
ai人工智能程序_简单解释:一个AI程序如何掌握Go的古老游戏
查看>>
以下是ECMAScript 2016、2017和2018中所有新增功能的示例
查看>>
印地语freeCodeCamp YouTube频道+不和谐聊天现已上线
查看>>
css content属性_CSS之前和之后CSS –如何使用content属性
查看>>
如何将ApexCharts与Vue.js集成-详细指南
查看>>
nsfw_如何使用机器学习设置NSFW内容检测
查看>>
react中使用构建缓存_如何使用React构建客户支持实时聊天小部件
查看>>
reactjs生命周期_如何在ReactJS中了解组件的生命周期方法
查看>>
graphql和rest_如何使用GraphQL使您的网站整洁和可维护
查看>>
hbuilder自述文件_为什么创建自述文件已成为我最喜欢的开发部分
查看>>
从ES5到ESNext-这是自2015年以来添加到JavaScript的所有功能
查看>>
GraphQL中的突变简介:它们是什么以及如何使用它们
查看>>
计算机编程课程顺序_550月份提供免费的在线编程和计算机科学课程
查看>>
哈希编码学习方法_想学习编码吗? 这很容易。 这是解决困难部分的方法。
查看>>
python基础知识教程_通过此深入的视频课程学习Python基础知识
查看>>
浙江省高考志愿填报志愿文档_成为freeCodeCamp的志愿者编辑的一年如何改变了我的生活
查看>>
批量删除推文_如何批量(免费)删除过去的推文,以及如何摆脱过去的自我
查看>>
Elasticsearch初学者教程
查看>>
组件和高阶组件区别_高阶组件:终极指南
查看>>