JSON (JavaScript Object notation) is a simple data format that is lighter than XML. JSON is the native format of JavaScript, which means that processing JSON data in JavaScript does not require any special APIs or toolkits.
The rule of JSON is simple: an object is an unordered set of ' name/value pairs '. An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends. Each "name" followed by a ":" (colon); "' Name/value ' pairs" (comma) separated
var YX = {
Get the type of JS built-in data type, the return value includes [Date,regexp,number,string,array,boolean,function,null,undefined,object]
Gettype:function (obj) {
return obj = null? obj + "": Object.prototype.toString.call (obj). Slice (8,-1);
}
To create a simple class
, Createclass:function () {
return function () {
This.init.apply (this,arguments);
};
}
format string, Yx.format ("{0},{1},haha", ["Hello", "World"])
, Format:function (str,params) {
var reg =/{(d+)}/g;
Return Str.replace (Reg,function (match,val) {
return Params[~~val];
});
}
format string, YX.FORMAT2 ("Math ={Math}, Language ={language},haha", {"Mathematics": 100, "Language": 99})
, Format2:function (str,params) {
var reg =/{([^{}]+)}/g;
Return Str.replace (Reg,function (match,val) {
return Params[val];
});
}
Format time, YX.FORMAT3 (new Date, "Yy-mm-dd h:m:s")
, Format3:function (Date,patten) {
var y = date.getfullyear (), Mon = Date.getmonth () + 1,d = Date.getdate (), H = date.gethours (), Min = Date.getminutes (), s = da Te.getseconds ()
, zero = function (o) {return ("0" +o). Slice (-2)}
, matchs = {"yy": Y, "y":(y+ ""). Slice ( -2), "MM": zero (Mon), "M": Mon, "dd": zero (d), "D":d, "HH": Zero (h), "H": H, "MM": zero ( min), "M": Min, "SS": zero (s), "s": s};
Return Patten.replace (/yy|y|mm|m|dd|d| hh| h| mm| m| ss| S/g,function (Match) {
return Matchs[match];
});
}
Array to Heavy, yx.unique ([1,1, "1", Document.body,document.body]
, Unique:function (arr) {
var kv = {},len = Arr.length,rs = [],t;
for (; len--;) {
t = Arr[len];
(kv[t]==undefined | | kv[t]!==t) && (Kv[t]=t,rs.push (t));
}
Return RS;
}
};
JSON provides a json.js package that is introduced into the http://www.json.org/json.js and then can be converted to JSON data simply by using object.tojsonstring ().