中文字幕理论片,69视频免费在线观看,亚洲成人app,国产1级毛片,刘涛最大尺度戏视频,欧美亚洲美女视频,2021韩国美女仙女屋vip视频

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開(kāi)通VIP
JSON in JavaScript

再翻譯一篇,早看過(guò)這篇了,只是這個(gè)里頭涉及到了JSON的驗(yàn)證,對(duì)JSONP是個(gè)補(bǔ)充,所以想翻譯一下。
JSON in JavaScript。

JSON是javascript的一個(gè)子集,因而在javascript中使用也不會(huì)有什么沖突。
看個(gè)例子:
var myJSONObject = {”bindings”: [
{”ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*”},
{”ircEvent”: “PRIVMSG”, “method”: “deleteURI”, “regex”: “^delete.*”},
{”ircEvent”: “PRIVMSG”, “method”: “randomURI”, “regex”: “^random.*”}
]
};

在這個(gè)例子中,創(chuàng)建了一個(gè)名為bindings的對(duì)象,該對(duì)象包含了一個(gè)包含3個(gè)對(duì)象的數(shù)組,每個(gè)對(duì)象都包含3個(gè)成員”ircEvent”, “method”, and “regex” 。
每個(gè)成員都可以通過(guò)點(diǎn)或者下標(biāo)來(lái)取得,比如:myJSONObject.bindings[0].method

要將JSON文本轉(zhuǎn)換成對(duì)象只要使用eval()函數(shù)就可以了,該函數(shù)會(huì)調(diào)用js編譯器。既然JSON是javascript的子集,那么js編譯器就會(huì)將其轉(zhuǎn)換成正確的對(duì)象:var myObject = eval(’(’ + myJSONtext + ‘)’);

eval()函數(shù)的運(yùn)行速度很快,而且它可以執(zhí)行任何的js代碼,因此可能會(huì)帶來(lái)一些安全方面的問(wèn)題。
當(dāng)eval()要執(zhí)行的代碼是安全的并且沒(méi)有錯(cuò)誤的時(shí)候,當(dāng)然沒(méi)有什么問(wèn)題的,通常這就是服務(wù)器同時(shí)提供JSON數(shù)據(jù)并且調(diào)用eval()函數(shù)的情況。當(dāng)然也會(huì)有eval()函數(shù)執(zhí)行的代碼不可信的情況。

當(dāng)執(zhí)行代碼不可靠時(shí),我們最好使用一個(gè)json的轉(zhuǎn)換器,這個(gè)轉(zhuǎn)換器會(huì)檢查json文本的安全性,這樣就安全的多了:
var myObject = JSON.parse(myJSONtext, filter);
filter 參數(shù)是可選的,它會(huì)檢查每個(gè)JSON鍵值對(duì),每個(gè)值都會(huì)被filter 函數(shù)的處理結(jié)果代替,這可以用來(lái)將一般對(duì)象轉(zhuǎn)換成我們需的其他對(duì)象,或者是將日期字符串轉(zhuǎn)換成日期對(duì)象:
var myData = JSON.parse(text, function (key, value) { returnkey.indexOf(’date’) >= 0 ? new Date(value) : value; });

而JSON字符串化器作用正好相反,它將Js對(duì)象裝換成JSON文本。JSON不支持循環(huán)的數(shù)據(jù)結(jié)構(gòu),因而不要把循環(huán)結(jié)構(gòu)拿來(lái)轉(zhuǎn)換。
var myJSONText = JSON.stringify(myObject);

這里有個(gè)轉(zhuǎn)換器的例子:
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;
}
}
}
}
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’);
}
};
}();
}

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
JavaScript中的JSON
JSON.stringify()、JSON.parse()和eval(string)
使用 Dojo 的 Ajax 應(yīng)用開(kāi)發(fā)進(jìn)階教程,第 1 部分: JavaScript 技巧...
JS操作JSON總結(jié)
JSON的簡(jiǎn)單使用
Json
更多類似文章 >>
生活服務(wù)
熱點(diǎn)新聞
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服