Document directory
- I. JSON. js
- II. It is still taken from D.C, which is in json_parse.js.
- 3. It is still big in D.C, in json_parse_state.js.
- 4. What is the old version of JSON. js and moontools ?. Here, what does/[\ x00-\ x1f \ "]/g mean?
- 5. My current solution is simple and clear, but will there be any unknown problems? To be clarified.
I. JSON. js
First, the douglascrockford solution should be adopted by many people. JSON. js and json2.js are similar.
// https://raw.github.com/douglascrockford/JSON-js/master/json.jsvar meta = { // table of character substitutions'\b' : '\\b','\t' : '\\t','\n' : '\\n','\f' : '\\f','\r' : '\\r','"' : '\\"','\\' : '\\\\'};function quote(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 escape// sequences.escapable.lastIndex = 0;return escapable.test(string) ? '"'+ string.replace(escapable, function(a) {var c = meta[a];return typeof c === 'string' ? c : '\\u'+ ('0000' + a.charCodeAt(0).toString(16)).slice(-4);}) + '"' : '"' + string + '"';}
II. It is still taken from D.C, which is in json_parse.js.
// https://raw.github.com/douglascrockford/JSON-js/master/json_parse.jsvar escapee = {'"' : '"','\\' : '\\','/' : '/',b : '\b',f : '\f',n : '\n',r : '\r',t : '\t'};string = function() {// Parse a string value.var hex, i, string = ', uffff;// When parsing for string values, we must look for " and \ characters.if (ch === '"') {while (next()) {if (ch === '"') {next();return string;} else if (ch === '\\') {next();if (ch === 'u') {uffff = 0;for (i = 0; i < 4; i += 1) {hex = parseInt(next(), 16);if (!isFinite(hex)) {break;}uffff = uffff * 16 + hex;}string += String.fromCharCode(uffff);} else if (typeof escapee[ch] === 'string') {string += escapee[ch];} else {break;}} else {string += ch;}}}error("Bad string");}
3. It is still big in D.C, in json_parse_state.js.
// https://raw.github.com/douglascrockford/JSON-js/master/json_parse_state.jsvar escapes = { // Escapement translation table'\\' : '\\','"' : '"','/' : '/','t' : '\t','n' : '\n','r' : '\r','f' : '\f','b' : '\b'};function debackslashify(text) {// Remove and replace any backslash escapement.return text.replace(/\\(?:u(.{4})|([^u]))/g, function(a, b, c) {return b ? String.fromCharCode(parseInt(b, 16)) : escapes[c];});}
4. What is the old version of JSON. js and moontools ?. Here, what does/[\ x00-\ x1f \ "]/g mean?
// JSON escape var special = {'\ B': '\ B', '\ t':' \ t', '\ N' with special characters ': '\ n',' \ F': '\ F',' \ R': '\ R ','"':'\\"', '\': '\'}; var escape = function (CHR) {return special [CHR] | '\ U' + ('000000' + Chr. charcodeat (0 ). tostring (16 )). slice (-4)}; function _ escape (OBJ) {return '"' + obj. replace (/[\ x00-\ x1f \ "]/g, escape) + '"'}
5. My current solution is simple and clear, but will there be any unknown problems? To be clarified.
function StringAs(string) {return '"' + string.replace(/(\\|\"|\n|\r|\t)/g, "\\$1") + '"';}
6. Finally found that there is a http://relucent.iteye.com/blog/646016