Object.prototype.deep_clone = function(){
eval("var tmp = " + this.toJSON());
return tmp;
}
Object.prototype.toJSON = function(){
var json = [];
for(var i in this){
if(!this.hasOwnProperty(i)) continue;
//if(typeof this[i] == "function") continue;
json.push(
i.toJSON() + " : " +
((this[i] != null) ? this[i].toJSON() : "null")
)
}
return "{\n " + json.join(",\n ") + "\n}";
}
Array.prototype.toJSON = function(){
for(var i=0,json=[];i<this.length;i++)
json[i] = (this[i] != null) ? this[i].toJSON() : "null";
return "["+json.join(", ")+"]"
}
String.prototype.toJSON = function(){
return '"' +
this.replace(/(\\|\")/g,"\\$1")
.replace(/\n|\r|\t/g,function(){
var a = arguments[0];
return (a == '\n') ? '\\n':
(a == '\r') ? '\\r':
(a == '\t') ? '\\t': ""
}) +
'"'
}
Boolean.prototype.toJSON = function(){return this}
Function.prototype.toJSON = function(){return this}
Number.prototype.toJSON = function(){return this}
RegExp.prototype.toJSON = function(){return this}
// strict but slow
String.prototype.toJSON = function(){
var tmp = this.split("");
for(var i=0;i<tmp.length;i++){
var c = tmp[i];
(c >= ' ') ?
(c == '\\') ? (tmp[i] = '\\\\'):
(c == '"') ? (tmp[i] = '\\"' ): 0 :
(tmp[i] =
(c == '\n') ? '\\n' :
(c == '\r') ? '\\r' :
(c == '\t') ? '\\t' :
(c == '\b') ? '\\b' :
(c == '\f') ? '\\f' :
(c = c.charCodeAt(),('\\u00' + ((c>15)?1:0)+(c%16)))
)
}
return '"' + tmp.join("") + '"';
}
分享到:
相关推荐
JavaScript and JSON Essentials is a step-by-step guide that will introduce you to JSON and help you understand how the lightweight JSON data format can be used in different ways either to store data ...
XML to JSON and JSON to XML converter
NULL 博文链接:https://shuaigg-babysky.iteye.com/blog/458603
在网页中想后台传递多个数据时,有时数据还是多个动态列表,数据很复杂时,JavaScript程序员喜欢把他们作为json串进行处理,后台收到后需要对json字符串进行解析,幸好有JSON-lib,这个Java类包用于把bean,map和XML...
Chapter 1, Getting Started with JSON, introduces the audience to JSON, discusses the history of JSON along with how it works and stores itself in memory. The chapter also outlines the popular ...
JSON (JavaScript Object Notation) is a lightweight text-based data interchange format used to create objects to transfer data over the Internet. It's widely used today by common web applications, as ...
这是非常有用的,我们将通过jQuery插件$.toJSON把数据对象转换为json格式。 使用JavaScript构建JSON格式字符串 JavaScript代码: 在这里包含了javascript代码。$(“#form”).submit(function(){}- delete_button是...
针对覆盖的Array.prototype.toJSON和IE8的Date.prototype.toJSON的非标准化版本对JavaScript JSON API进行Date.prototype.toJSON 。 相对于优势 小得多,只有几行而不是4KB 使用本机的stringify和parse方法 在较旧...
添加Error.toJSON(),以便我们可以轻松地对错误进行字符串化,例如,通过websocket发送错误时 // just do it before anything else require('error-tojson'); 该软件包与MongoDB本机驱动程序不兼容。 每当mongo...
JSON is an established standard format used to exchange data. This book shows how JSON plays different roles in full web development using various examples. By the end of this book, you'll have a new ...
JSON (JavaScript Object Notation) is a lightweight text-based data interchange format used to create objects to transfer data over the Internet. It's widely used today by common web applications, as ...
地图.prototype.tojson ES7 map.prototype.tojson polyfill安装 $ npm install --save map.prototype.tojson用法 require ( 'map.prototype.tojson' ) ;var myMap = new Map ( ) ;myMap . set ( NaN , "not a number...
ngraph.tojson 将图形存储为简单 json 格式的库 用法 var graph = require ( 'ngraph.graph' ) ( ) ; graph . addLink ( 'hello' , 'world' ) ; var toJSON = require ( 'ngraph.tojson' ) ; console . log ( to...
在javascript中将json字符串或对象发射成xml的对象或字符串的相关方法操作,反之也有
set.prototype.tojson 用于set.prototype.toJSON() ES7 set.prototype.toJSON()安装 $ npm install --save set.prototype.tojson用法 require ( 'set.prototype.tojson' ) ;var mySet = new Set ( ) ;mySet . add ( ...
json转换json schema
表单生成器 用于拖放表单创建的 jQuery 插件 用法 要开始使用此插件构建表单,只需在要制作编辑器的文本区域上调用formBuilder()即可。 FormBuilder 接受许多选项,并且可以通过这些选项进行翻译。...
用法 // To JSONvar json = CSSJSON.toJSON(cssString);// To CSSvar css = CSSJSON.toCSS(jsonObject);样本见example.htmlJSON { "children": { "@media (max-width: 800px)": { "children": { "#main #comments": ...
whc-json-to-class whc-json-to-class是javascript插件,可自动将json字符串转换为相应的语言模型类演示要求JavaScript ES6 +支持语言WHCParserLanguage.Dart WHCParserLanguage.Swift WHCParserLanguage.SwiftClass...