Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What is JSON+ and how to handle JSON strings?
What is JSON+ and how to handle JSON strings?
JSON is a format.

Different types of variables are converted into JSON format in slightly different ways.

(1) It is very simple to convert numeric, Boolean variables and function objects into JSON format without any processing.

(2) When converting string variables into JSON format, we need to deal with some special characters first. For example, double quotes, single quotes, carriage returns and line breaks are replaced by backslashes.

, and then enclose the converted content in quotation marks. The code is as follows:

return(" " " "+obj . replace(/([" " " " '])/g," " " $ 1 ")。 Replace (/"r/, "r"). Replace (/"n/, """n"). Replace (/"t/, ""t ")+"""");

(3) When date-time variables are converted into JSON format, special treatment is required. The code is as follows:

Return ("(new date (""+obj.toutcstring ()+""));

(4) When converting the regular expression into JSON format, get its content through the source of this object. Not only do you need to deal with the special characters in the source code, but you also need to pay attention to the settings.

Global, ignoreCase and multiline attributes, the code is as follows:

return("(new RegExp(" "+String(obj . source))。 Replace (/("W)/g, ""$ 1")+ "",

" " "+(obj.global?" g ":" "+(obj . ignore case?" I ":" "+(obj . multiline?" m ":" " "+" " "));

(5) When converting the array object into JSON format, recursively call the toJSONString function to serialize each element in turn, and then connect the obtained strings with commas. The outermost layer

Enclosed in square brackets [], the code is as follows:

var re = new Array();

for(var I = 0; I < object length; i++)re . push(toJSONString(obj[I]));

return("[" + re.join(","+"]");

(6) When converting an object into JSON format, similar to an array object, it is also necessary to process each component element in turn through recursive calls. In addition, it should be pointed out that

Yes, when its constituent elements are serialized, they need to form a name-value pair.