📄 分析json_js(注释) - 网站 学习 - 博客园.mht
字号:
<P> =20
string.parseJSON(hook)</P>
<P> =
This=20
method parses a JSON text to produce an object or array. It can throw a=20
SyntaxError exception.</P>
<P>[=E6=B3=A8=EF=BC=9A=E4=B8=8A=E9=9D=A2=E8=AF=B4=E5=88=B0=E5=85=B3=E9=94=
=AE=E9=97=AE=E9=A2=98=E4=BA=86=EF=BC=8CJavascr=C4=ABpt=E4=B8=AD=E7=9A=84=E5=
=AF=B9=E8=B1=A1=E5=8F=AF=E4=BB=A5=E8=A2=AB=E8=BD=AC=E5=8C=96=E4=B8=BAJSON=
Text=EF=BC=8C=E9=82=A3=E4=B9=88JSON=20
Text=E5=8F=AF=E4=BB=A5=E7=BB=8F=E8=BF=87=E9=80=86=E8=BF=87=E7=A8=8B=E8=BD=
=AC=E5=8C=96=E4=B8=BAJavascr=C4=ABpt=E7=9A=84=E5=AF=B9=E8=B1=A1=EF=BC=8C<=
/P>
<P>=E5=8F=AF=E6=98=AFDate=E5=AF=B9=E8=B1=A1=E6=98=AF=E9=80=86=E4=B8=8D=E5=
=9B=9E=E6=9D=A5=E7=9A=84=EF=BC=8C=E5=A6=82</P>
<P>alert(new Date().toJSONString().parseJSON());</P>
<P>]</P>
<P> =
The=20
optional hook parameter is a function which can filter and transform the =
results. It receives each of the values, and its return value is =
used=20
instead. If it returns what it received, then structure is not =
modified.</P>
<P>[</P>
<P>=E6=B3=A8=EF=BC=9A=E4=B8=8A=E9=9D=A2=E8=AF=B4=EF=BC=8C=E6=B3=A8=E5=85=A5=
=E5=88=B0String=E7=B1=BB=E4=B8=AD=E7=9A=84=E5=AF=B9=E8=B1=A1=E6=96=B9=E6=B3=
=95parseJSON=E6=9C=89=E4=B8=AA=E5=8F=AF=E9=80=89=E7=9A=84=E5=8F=82=E6=95=B0=
=EF=BC=8Chook=EF=BC=8C=E8=BF=99=E4=B8=AA=E5=8F=82=E6=95=B0=E6=98=AF=E4=B8=
=AA=E9=92=A9=E5=AD=90=E5=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8=BF=9B=E8=A1=8C=E5=9B=
=9E=E8=B0=83=E7=9A=84=E6=9C=BA=E5=88=B6=E3=80=82=E8=BF=99=E4=B8=9C=E8=A5=BF=
=E5=9C=A8<A=20
onclick=3D"javascript:tagshow(event, 'java');" href=3D"javascript:;"=20
target=3D_self><U><STRONG>java</STRONG></U></A>=E4=B8=AD=E5=8F=AA=E8=83=BD=
=E9=80=9A=E8=BF=87=E6=8E=A5=E5=8F=A3=E6=9D=A5=E5=AE=9E=E7=8E=B0=E3=80=82<=
/P>
<P> </P>
<P>]</P>
<P> =20
Example:</P>
<P> // =
Parse=20
the text. If it contains any "NaN" strings, replace=20
them<BR>  =
; //=20
with the NaN value. All other values are left alone.</P>
<P> =
myData =3D=20
text.parseJSON(function (value)=20
{<BR> &n=
bsp; =20
if (value =3D=3D=3D 'NaN')=20
{<BR> &n=
bsp; =20
return=20
NaN;<BR>  =
; =20
}<BR> &n=
bsp; =20
return=20
value;<BR> &nb=
sp;=20
});</P>
<P> </P>
<P> It is expected that these methods will formally =
become=20
part of the Javascr=C4=ABpt Programming Language in the Fourth Edition =
of the</P>
<P> ECMAscr=C4=ABpt standard in 2007.</P>
<P>[=E6=B3=A8=EF=BC=9A=E8=BF=99=E9=87=8C=E6=8F=90=E5=87=BA=E4=BA=86JSON.j=
s=E4=BD=9C=E8=80=85=E4=BB=AC=E7=9A=84=E5=B8=8C=E6=9C=9B=E3=80=82=E4=BB=96=
=E4=BB=AC=E5=B8=8C=E6=9C=9B=E8=BF=99=E4=BA=9B=E6=96=B9=E6=B3=95=E8=83=BD=E5=
=A4=9F=E6=88=90=E4=B8=BAJavascr=C4=ABpt=E8=AF=AD=E8=A8=80=E4=B8=AD=E6=AD=A3=
=E5=BC=8F=E7=9A=84=E4=B8=80=E9=83=A8=E5=88=86=E3=80=82]</P>
<P>*/</P>
<P> </P>
<P>//=E6=98=AF=E5=90=A6=E5=B7=B2=E7=BB=8F=E5=9C=A8Object=E4=B8=AD=E5=AE=9A=
=E4=B9=89=E4=BA=86toJSONString?</P>
<P>//=E6=B2=A1=E6=9C=89=E7=9A=84=E8=AF=9D=E5=86=8D=E5=88=9B=E5=BB=BA=E3=80=
=82<BR>if (!Object.prototype.toJSONString) {</P>
<P> =
//=E5=BE=80Array=E4=B8=AD=E6=B3=A8=E5=85=A5toSONString=E6=96=B9=E6=B3=95<=
/P>
<P> Array.prototype.toJSONString =3D function () {</P>
<P> var a =3D ['['], b, i, l =
=3D=20
this.length, v;</P>
<P> =
//=E5=86=85=E9=83=A8=E5=87=BD=E6=95=B0=EF=BC=8C=E5=8F=AF=E4=BB=A5=E4=BD=BF=
=E7=94=A8=E5=88=B0=E5=A4=96=E9=83=A8=E5=AE=9A=E4=B9=89=E7=9A=84a,b=E7=AD=89=
=E5=8F=98=E9=87=8F</P>
<P> function p(s) {</P>
<P><BR> =
if=20
(b)</P>
<P> =20
{<BR> &n=
bsp; =20
a.push(',');<BR> &nb=
sp; =20
}</P>
<P><BR> =
=20
a.push(s);<BR>  =
; =20
b =3D true;<BR> }</P>
<P> </P>
<P> for (i =3D 0; i < l; i =
+=3D 1)=20
{<BR> =
v =3D=20
this[i];<BR> &=
nbsp;=20
switch (typeof v) {</P>
<P> =20
//=E5=A6=82=E6=9E=9Carray=E4=B8=AD=E7=9A=84=E6=88=90=E5=91=98=E6=98=AFund=
efined/function/unknow=E7=9A=84=E8=AF=9D</P>
<P> =20
//=E5=88=99=E7=9B=B4=E6=8E=A5=E8=BF=94=E5=9B=9E=EF=BC=8C=E4=B9=9F=E5=B0=B1=
=E6=98=AF=E4=B8=8D=E5=A4=84=E7=90=86=E4=BA=86<BR> =
=20
case=20
'undefined':<BR> &nb=
sp; =20
case=20
'function':<BR> &nbs=
p; =20
case=20
'unknown':<BR>  =
; =20
break;</P>
<P> //=E6=98=AFobjec=
t=E7=B1=BB=E5=9E=8B=E7=9A=84=E8=AF=9D,=E5=81=9A=E9=80=92=E5=BD=92=E8=A7=A3=
=E6=9E=90=E4=BA=86<BR> &nb=
sp; =20
case 'object':</P>
<P> &nbs=
p; =20
if (v)</P>
<P> &nbs=
p; {<BR> =
=20
if (typeof v.toJSONString =3D=3D=3D 'function') =20
{<BR> &n=
bsp; =20
p(v.toJSONString());<BR> &=
nbsp; =20
}<BR> &n=
bsp; =20
}</P>
<P> &nbs=
p; =20
else</P>
<P> &nbs=
p; {<BR> =
=20
p("null");<BR>  =
; }</P>
<P><BR> =
=20
break;</P>
<P><BR> =
=20
default:</P>
<P> &nbs=
p; =20
p(v.toJSONString());</P>
<P><BR> =
=20
}<BR> =20
}<BR> =20
a.push(']');<BR> return=20
a.join('');<BR> };</P>
<P> </P>
<P> =
//=E5=BE=80boolean=E4=B8=AD=E6=B3=A8=E5=85=A5toJSONString=E6=96=B9=E6=B3=95=
</P>
<P> Boolean.prototype.toJSONString =3D function ()=20
{<BR> return=20
String(this);<BR> };</P>
<P> =
//=E5=BE=80Date=E4=B8=AD=E6=B3=A8=E5=85=A5toJSONString=E6=96=B9=E6=B3=95<=
/P>
<P> Date.prototype.toJSONString =3D function () {</P>
<P> function f(n)=20
{<BR> =
return n=20
< 10 ? '0' + n : n;<BR> =
}</P>
<P> return '"' + =
this.getFullYear() +=20
'-'=20
+<BR> &n=
bsp; =20
f(this.getMonth() + 1) + '-'=20
+<BR> &n=
bsp; =20
f(this.getDate()) + 'T'=20
+<BR> &n=
bsp; =20
f(this.getHours()) + ':'=20
+<BR> &n=
bsp; =20
f(this.getMinutes()) + ':'=20
+<BR> &n=
bsp; =20
f(this.getSeconds()) + '"';<BR> };</P>
<P> Number.prototype.toJSONString =3D function ()=20
{<BR> return isFinite(this) ?=20
String(this) : "null";<BR> };</P>
<P> </P>
<P> //=E6=95=B0=E7=BB=84=E8=A2=AB=E8=BD=AC=E5=8C=96=E4=B8=BA=E4=BB=A5=
[]=E5=8C=85=E5=9B=B4=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=8C=E5=AF=B9=
=E8=B1=A1=E7=9A=84=E8=AF=9D=EF=BC=8C=E5=88=99=E8=BD=AC=E5=8C=96=E4=B8=BA{=
=E5=8C=85=E5=9B=B4=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2}</P>
<P> Object.prototype.toJSONString =3D function ()=20
{<BR> var a =3D ['{'], b, i, =
v;</P>
<P> function p(s)=20
{<BR> =
if (b)=20
{<BR> &n=
bsp; =20
a.push(',');<BR> &nb=
sp; =20
}<BR> =20
a.push(i.toJSONString(), ':',=20
s);<BR> =
b =3D=20
true;<BR> }</P>
<P> for (i in this)=20
{<BR> =
if=20
(this.hasOwnProperty(i))=20
{<BR> &n=
bsp; =20
v =3D=20
this[i];<BR> &=
nbsp; =20
switch (typeof v)=20
{<BR> &n=
bsp; =20
case=20
'undefined':<BR> &nb=
sp; =20
case=20
'function':<BR> &nbs=
p; =20
case=20
'unknown':<BR>  =
; =20
break;<BR> &nb=
sp; =20
case=20
'object':<BR> =
=20
if (v)=20
{<BR> &n=
bsp; =20
if (typeof v.toJSONString =3D=3D=3D 'function')=20
{<BR> &n=
bsp; &nb=
sp; =20
p(v.toJSONString());<BR> &=
nbsp; &n=
bsp; =20
}<BR> &n=
bsp; =20
} else=20
{<BR> &n=
bsp; =20
p("null");<BR>  =
; =20
}<BR> &n=
bsp; =20
break;<BR> &nb=
sp; =20
default:<BR> &=
nbsp; =20
p(v.toJSONString());<BR> &=
nbsp; =20
}<BR> =20
}<BR> =20
}<BR> =20
a.push('}');<BR> return=20
a.join('');<BR> };</P>
<P> </P>
<P> </P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -