首先
/*
Public Domain
NO WARRANTY EXPRESSED OR IMPLIED
See
This code should be minified before deployment
See
USE YOUR OWN COPY
NOT CONTROL
This file creates a global JSON object containing two methods: stringify
and parse
JSON
value any JavaScript value
replacer an optional parameter that determines how object
values are stringified for objects
function or an array of strings
space an optional parameter that specifies the indentation
of nested structures
be packed without extra whitespace
it will specify the number of spaces to indent at each
level
it contains the characters used to indent at each level
This method produces a JSON text from a JavaScript value
When an object value is found
method
stringified
value represented by the name/value pair that should be serialized
or undefined if nothing should be serialized
will be passed the key associated with the value
bound to the value
For example
Date
function f(n) {
// Format integers to have at least two digits
return n <
}
return this
f(this
f(this
f(this
f(this
f(this
};
You can provide an optional replacer method
key and value of each member
object
serialized
be excluded from the serialization
If the replacer parameter is an array of strings
used to select the members to be serialized
such that only members with keys listed in the replacer array are
stringified
Values that do not have JSON representations
functions
dropped; in arrays they will be replaced with null
a replacer function to replace those with JSON values
JSON
The optional space parameter produces a stringification of the
value that is filled with line breaks and indentation to make it
easier to read
If the space parameter is a non
be used for indentation
the indentation will be that many spaces
Example:
text = JSON
// text is
text = JSON
// text is
text = JSON
return this[key] instanceof Date ?
});
// text is
JSON
This method parses a JSON text to produce an object or array
It can throw a SyntaxError exception
The optional reviver parameter is a function that can filter and
transform the results
and its return value is used instead of the original value
If it returns what it received
If it returns undefined then the member is deleted
Example:
// Parse the text
// be converted to Date objects
myData = JSON
var a;
if (typeof value ===
a =
/^(d{
if (a) {
return new Date(Date
+a[
}
}
return value;
});
myData = JSON
var d;
if (typeof value ===
value
value
d = new Date(value
if (d) {
return d;
}
}
return value;
});
This is a reference implementation
redistribute
*/
/*jslint evil: true
/*members ""
call
getUTCMinutes
lastIndex
test
*/
// Create a JSON object only if one does not already exist
// methods in a closure to avoid creating global variables
if (!this
{
this
}
(function () {
"use strict";
function f(n) {
// Format integers to have at least two digits
return n <
}
if (typeof Date
Date
return isFinite(this
this
f(this
f(this
f(this
f(this
f(this
};
String
Number
Boolean
return this
};
}
var cx = /[u
escapable = /["x
gap
indent
meta = { // table of character substitutions
}
rep;
function quote(string) {
escapable
return escapable
var c = meta[a];
return typeof c ===
}) +
}
function str(key
var i
k
v
length
mind = gap
partial
value = holder[key];
if (value && typeof value ===
typeof value
value = value
}
if (typeof rep ===
value = rep
}
switch (typeof value) {
case
return quote(value);
case
return isFinite(value) ? String(value) :
case
case
return String(value);
case
if (!value) {
return
}
gap += indent;
partial = [];
if (Object
length = value
for (i =
partial[i] = str(i
}
v = partial
gap ?
partial
mind +
gap = mind;
return v;
}
if (rep && typeof rep ===
length = rep
for (i =
k = rep[i];
if (typeof k ===
v = str(k
if (v) {
partial
}
}
}
} else {
for (k in value) {
if (Object
v = str(k
if (v) {
partial
}
}
}
}
v = partial
gap ?
mind +
gap = mind;
return v;
}
}
if (typeof JSON
JSON
var i;
gap =
indent =
if (typeof space ===
for (i =
indent +=
}
} else if (typeof space ===
indent = space;
}
rep = replacer;
if (replacer && typeof replacer !==
(typeof replacer !==
typeof replacer
throw new Error(
}
return str(
};
}
if (typeof JSON
JSON
var j;
function walk(holder
var k
if (value && typeof value ===
for (k in value) {
if (Object
v = walk(value
if (v !== undefined) {
value[k] = v;
} else {
delete value[k];
}
}
}
}
return reviver
}
text = String(text);
cx
if (cx
text = text
return
(
});
}
if (/^[]
j = eval(
return typeof reviver ===
walk({
}
throw new SyntaxError(
};
}
}());
第二
注意
<script src="
<script src="
var a =
alert("用於組成json的字符串:【"+a+"】");
var b = JSON
var c = JSON
alert("轉換後的字符串
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20282.html