- 浏览: 421062 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
jxausea:
java.io.IOException: Keystore w ...
一个opoenfire与smack的例子 -
517913840:
请问 为什么我用roster.getPresence取用户的在 ...
一个opoenfire与smack的例子 -
cymmint:
请了,大哥
轻松利用JQuery实现ajax跨域访问 -
lishujuncat:
多谢,学下了
一个opoenfire与smack的例子 -
InSoNia:
不错
最简单的ajax例子
JavaScript is a general purpose programming language that was introduced as the page scripting language for Netscape Navigator. It is widely believed to be a subset of Java, but it is not. It is a Scheme-like language with C-like syntax and soft objects. JavaScript was standardized in the ECMAScript Language Specification, Third Edition.
JSON 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.
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.
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.
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 and competent. 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 recognize only JSON text and so is much safer:
The optional filter parameter is a function that will be called for every key and value at every level of the final result. Each value will be replaced by the result of the filter function. This can be used to reform generic objects into instances of classes, or to transform date strings into Date objects.
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.
If the stringify method sees an object that contains a toJSON method, it calls the method, and stringifies the value returned. This allows an object to determine its own JSON representation.
The stringifier method can take an optional array of strings. These strings are used to select the properties that will be included in the JSON text. Otherwise, all of the properties of the object will be included. In any case, values that do not have a representation in JSON (such as functions and undefined) are excluded.
The open source code of a JSON parser and JSON stringifier is available. When minified it is less than 2K.
JSON 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 and competent. 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 recognize only JSON text and so is much safer:
var myObject = JSON.parse(myJSONtext, filter);
The optional filter parameter is a function that will be called for every key and value at every level of the final result. Each value will be replaced by the result of the filter function. This can be used to reform generic objects into instances of classes, or to transform date strings into Date objects.
myData = JSON.parse(text, function (key, value) { return key.indexOf('date') >= 0 ? new Date(value) : value; });
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 = JSON.stringify(myObject);
If the stringify method sees an object that contains a toJSON method, it calls the method, and stringifies the value returned. This allows an object to determine its own JSON representation.
The stringifier method can take an optional array of strings. These strings are used to select the properties that will be included in the JSON text. Otherwise, all of the properties of the object will be included. In any case, values that do not have a representation in JSON (such as functions and undefined) are excluded.
The open source code of a JSON parser and JSON stringifier is available. When minified it is less than 2K.
/* json2.js 2008-02-14 Public Domain No warranty expressed or implied. Use at your own risk. See http://www.JSON.org/js.html This file creates a global JSON object containing two methods: JSON.stringify(value, whitelist) value any JavaScript value, usually an object or array. whitelist an optional array parameter that determines how object values are stringified. This method produces a JSON text from a JavaScript value. There are three possible ways to stringify an object, depending on the optional whitelist parameter. If an object has a toJSON method, then the toJSON() method will be called. The value returned from the toJSON method will be stringified. Otherwise, if the optional whitelist parameter is an array, then the elements of the array will be used to select members of the object for stringification. Otherwise, if there is no whitelist parameter, then all of the members of the object will be stringified. Values that do not have JSON representaions, such as undefined or functions, will not be serialized. Such values in objects will be dropped; in arrays will be replaced with null. JSON.stringify(undefined) returns undefined. Dates will be stringified as quoted ISO dates. Example: var text = JSON.stringify(['e', {pluribus: 'unum'}]); // text is '["e",{"pluribus":"unum"}]' JSON.parse(text, filter) This method parses a JSON text to produce an object or array. It can throw a SyntaxError exception. The optional filter parameter is a function that can filter and transform the results. It receives each of the keys and values, and its return value is used instead of the original value. If it returns what it received, then structure is not modified. If it returns undefined then the member is deleted. Example: // Parse the text. If a key contains the string 'date' then // convert the value to a date. myData = JSON.parse(text, function (key, value) { return key.indexOf('date') >= 0 ? new Date(value) : value; }); This is a reference implementation. You are free to copy, modify, or redistribute. Use your own copy. It is extremely unwise to load third party code into your pages. */ /*jslint evil: true */ /*global JSON */ /*members "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply, charCodeAt, floor, getUTCDate, getUTCFullYear, getUTCHours, getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join, length, parse, propertyIsEnumerable, prototype, push, replace, stringify, test, toJSON, toString */ if (!this.JSON) { JSON = function () { function f(n) { // Format integers to have at least two digits. return n < 10 ? '0' + n : n; } Date.prototype.toJSON = function () { // Eventually, this method will be based on the date.toISOString method. return this.getUTCFullYear() + '-' + f(this.getUTCMonth() + 1) + '-' + f(this.getUTCDate()) + 'T' + f(this.getUTCHours()) + ':' + f(this.getUTCMinutes()) + ':' + f(this.getUTCSeconds()) + 'Z'; }; var m = { // table of character substitutions '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\' }; function stringify(value, whitelist) { var a, // The array holding the partial texts. i, // The loop counter. k, // The member key. l, // Length. r = /["\\\x00-\x1f\x7f-\x9f]/g, v; // The member value. switch (typeof value) { case 'string': // If the string contains no control characters, no quote characters, and no // backslash characters, then we can safely slap some quotes around it. // Otherwise we must also replace the offending characters with safe sequences. return r.test(value) ? '"' + value.replace(r, function (a) { var c = m[a]; if (c) { return c; } c = a.charCodeAt(); return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); }) + '"' : '"' + value + '"'; case 'number': // JSON numbers must be finite. Encode non-finite numbers as null. return isFinite(value) ? String(value) : 'null'; case 'boolean': case 'null': return String(value); case 'object': // Due to a specification blunder in ECMAScript, // typeof null is 'object', so watch out for that case. if (!value) { return 'null'; } // If the object has a toJSON method, call it, and stringify the result. if (typeof value.toJSON === 'function') { return stringify(value.toJSON()); } a = []; if (typeof value.length === 'number' && !(value.propertyIsEnumerable('length'))) { // The object is an array. Stringify every element. Use null as a placeholder // for non-JSON values. l = value.length; for (i = 0; i < l; i += 1) { a.push(stringify(value[i], whitelist) || 'null'); } // Join all of the elements together and wrap them in brackets. return '[' + a.join(',') + ']'; } if (whitelist) { // If a whitelist (array of keys) is provided, use it to select the components // of the object. l = whitelist.length; for (i = 0; i < l; i += 1) { k = whitelist[i]; if (typeof k === 'string') { v = stringify(value[k], whitelist); if (v) { a.push(stringify(k) + ':' + v); } } } } else { // Otherwise, iterate through all of the keys in the object. for (k in value) { if (typeof k === 'string') { v = stringify(value[k], whitelist); if (v) { a.push(stringify(k) + ':' + v); } } } } // Join all of the member texts together and wrap them in braces. return '{' + a.join(',') + '}'; } } return { stringify: stringify, parse: function (text, filter) { var j; function walk(k, v) { var i, n; if (v && typeof v === 'object') { for (i in v) { if (Object.prototype.hasOwnProperty.apply(v, [i])) { n = walk(i, v[i]); if (n !== undefined) { v[i] = n; } else { delete v[i]; } } } } return filter(k, v); } // Parsing happens in three stages. In the first stage, we run the text against // regular expressions that look for non-JSON patterns. We are especially // concerned with '()' and 'new' because they can cause invocation, and '=' // because it can cause mutation. But just to be safe, we want to reject all // unexpected forms. // We split the first stage into 4 regexp operations in order to work around // crippling inefficiencies in IE's and Safari's regexp engines. First we // replace all backslash pairs with '@' (a non-JSON character). Second, we // replace all simple value tokens with ']' characters. Third, we delete all // open brackets that follow a colon or comma or that begin the text. Finally, // we look to see that the remaining characters are only whitespace or ']' or // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval. if (/^[\],:{}\s]*$/.test(text.replace(/\\./g, '@'). replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'). replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { // In the second stage we use the eval function to compile the text into a // JavaScript structure. The '{' operator is subject to a syntactic ambiguity // in JavaScript: it can begin a block or an object literal. We wrap the text // in parens to eliminate the ambiguity. j = eval('(' + text + ')'); // In the optional third stage, we recursively walk the new structure, passing // each name/value pair to a filter function for possible transformation. return typeof filter === 'function' ? walk('', j) : j; } // If the text is not JSON parseable, then a SyntaxError is thrown. throw new SyntaxError('parseJSON'); } }; }(); }
发表评论
-
javascript url 传递参数中文乱码问题解决方案
2010-03-04 16:49 6058本文来自http://hi.baidu.com/%B4%FA% ... -
javascript创建节点div时,设置float属性不能生效
2009-09-01 10:41 1568好像浮动属性不能写成diva.style.float来改 . ... -
关于javascript的Array对象
2009-07-09 08:23 1377//Javascript中Array的默认方法里没有提供ind ... -
javascript离开当前页面就出发事件
2009-07-06 14:27 3303<!DOCTYPE HTML PUBLIC " ... -
用javascript向多选框添加监听事件!
2009-06-09 17:41 3055<html> <body> ... -
(网页一开始就加载)用JS弹出网页对话框后,后面那一层变灰,变成不可操做。
2009-02-27 15:33 0(可参考发布的一篇-用JS弹出网页对话框后,后面那一层变灰,变 ... -
替代全角和半角括号的方法示例
2009-02-26 14:36 3795<html> <head> & ... -
javascript中的各种输入限制
2009-02-26 14:19 1509转自http://www.blogjava.net/dream ... -
有关层定位、显示、隐藏的例子
2009-02-11 17:12 2308要移动层(把层移动到某个位置)就必须先将层设置绝对定位 sty ... -
想把某些内容固定在某些地方总是显示当前页面上
2008-12-16 16:23 1408作用:当在一个内容很长的页面中,想把某些内容固定在某些地方总是 ... -
css细线表格
2008-10-08 17:12 1458<style type="text/css ... -
jquery手记
2008-09-12 14:03 2645JQuery拿取对象的方式 $(‘#id’) :通过元素的id ... -
css细线表格
2008-09-03 10:21 2386<style type="text/css&q ... -
用JS弹出网页对话框后,后面那一层变灰,变成不可操做。
2008-08-21 17:21 6131<html> <head> & ... -
javascript+CSS下拉菜单演示
2008-08-21 15:15 3288<!DOCTYPE html PUBddC " ... -
javascript 显示剩余的时间
2008-08-15 09:05 2339function formatTime(reallyTime) ... -
javascript屏蔽按健
2008-08-05 09:00 1135document.oncontextmenu=function ... -
javascript时间差
2008-07-31 14:38 1934var d1=new Date(2008,7,10 ... -
window.open() 父子页面的传参&传值问题
2008-06-30 11:25 37613window.open() 传参问题: 父页面中: windo ... -
firefox并不支持selectSingleNode和selectNodes的解决方法
2008-06-30 09:27 5486function test(){ var perid = ...
相关推荐
钱包连接注册表WalletConnect协议的应用注册表提交新应用转到上方的“问题”标签按“新发行” 选择“应用提交” 填写模板提交新期原料药// Dappshttps: //registry.walletconnect.org/data/dapps.json// ...
json3.js 【JS / JavaScript 中解析JSON的js包,JSON官方的JSON解析包】。JavaScript中解析JSON的js包,页面中引入json3.js,即可使用。 使用方法:JSON.parse(str), JSON.stringify(obj) 更多详情请参考博文: ...
echarts.registerMap('ChinaCounties', require('./全国各县市json地图')); // 创建图表配置 var option = { series: [{ type: 'map', mapType: 'ChinaCounties', data: [ // 数据源,可包含地区名称和对应值 ...
echarts.registerMap('china', require('./china.json')); ``` 6. 更新配置并显示地图:根据实际需求,你可以添加系列数据来展示地图上的数据信息,如省份的数值统计。完成后,使用`setOption`方法更新配置并显示...
2. **JSON格式**:JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。JSON格式通常用于网络应用程序之间传输数据,例如,...
而JSON(JavaScript Object Notation)是轻量级的数据交换格式,它使得人们可以轻松地阅读和编写数据,同时也易于机器解析和生成。本案例将探讨如何通过AJAX向本地服务器发起GET和POST请求,以获取或交互JSON数据。 ...
在这个例子中,`.` 表示默认导入,指向`index.js`,`./foo`指向`foo.js`,而`./bar`根据导入方式(import或default)指向不同的文件。 4. 自定义入口文件`index.js` 在Vue3项目中,你可能希望根据项目需求自定义...
<img src="./screenshot/screenshot-2.jpg" alt="screenshot" width="980" style="border:1px solid #979797;"> ## 功能特性 - 美化JSON - JavaScript Object 对象转换 JSON - 待续 ## 下载安装 你可以...
jQuery Autocomplete是一款非常实用的JavaScript插件,它允许用户在输入框中输入文字时,根据已有的数据集动态提供补全建议。这个功能在许多Web应用中被广泛使用,如搜索框、表单输入等。在给定的“jquery ...
http://www.json.org/提供了一个json.js,这样ie8(兼容模式),ie7和ie6就可以支持JSON对象以及其stringify()和parse()方法; 可以在https://github.com/douglascrockford/JSON-js上获取到这个js,一般现在用json2....
as3corelib是一个强大的ActionScript 3库,它包含了一系列实用工具类,其中一个重要的功能就是处理JSON(JavaScript Object Notation)数据。JSON是一种轻量级的数据交换格式,广泛用于Web服务和客户端之间的数据...
1. **JSON**:JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,通常用于在服务器和客户端之间传递数据,如Web API请求和响应。它基于JavaScript的一个子集,语法简洁且易于理解。 2. **Newtonsoft....
http://datavmap-public.oss-cn-hangzhou.aliyuncs.com/areas/csv/100000_province.json 市(130000 河北省可更改) http://datavmap-public.oss-cn-hangzhou.aliyuncs.com/areas/csv/130000_city.json 县/区...
jwks-rsa 一个从JWKS(JSON Web密钥集)端点检索签名密钥的库。 npm install-保存jwks-rsa用法您将为客户端提供JWKS端点,该端点公开您的签名密钥。 然后,使用getSigningKey可以获取与特定kid匹配的签名密钥。 ...
确保`require('./mapJson/henan.json')`指向了你解压后的JSON文件路径。这样,ECharts就能识别河南省的地理信息,按照JSON中的数据渲染地图。 在实际应用中,你可能还需要根据业务需求添加交互功能,比如点击某个...
JSON2.js是由Douglas Crockford开发的一个JavaScript库,专门用于处理JSON数据,尤其在老版本的浏览器中,这些浏览器可能不支持原生的JSON解析和序列化功能。 在JavaScript中,JSON主要用于对象与字符串之间的转换...
**jqGrid:四、远程数据(JSON)** jqGrid是一个强大的JavaScript库,专门用于创建交互式的HTML表格。它能够从服务器获取数据,并将其呈现为可排序、可搜索、可分页的网格。在本篇文章中,我们将深入探讨jqGrid如何...
在Java开发中,`org.json.JSONObject` 是一个广泛使用的库,它允许开发者处理JSON对象,进行JSON数据的创建、解析和操作。这个库是`org.json`包的一部分,由Morten Kjetland开发并维护,它提供了一个简单且直观的...
JSON(JavaScript Object Notation http://www. json .org/ json -zh.html ),是一种轻量级的基于文本且独立于语言的数据交换格式,比 XML 更轻巧,它是 XML 数据交换的一个替代方案。它源于 ECMAScript 程序语言...