📄 json 入门指南.htm
字号:
<CODE>StackOverflowException</CODE>:</P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=code-outline><PRE class=displaycode>
@Test(expected=StackOverflowError.class)
public void testRecurrsiveMap2Json() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("key", map);
JsonUtil.map2Json(map);
}
</PRE></TD></TR></TBODY></TABLE><BR>
<P>好在服务器处理的 JSON 数据最终都应该转化为简单的 JavaScript 对象,因此,递归引用的可能性很小。 </P>
<P>最后,通过 Servlet 或 MVC 框架输出 JSON 时,需要设置正确的 MIME
类型(application/json)和字符编码。假定服务器使用 UTF-8 编码,则可以使用以下代码输出编码后的 JSON
文本:</P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=code-outline><PRE class=displaycode>
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
pw.write(JsonUtil.toJson(obj));
pw.flush();
</PRE></TD></TR></TBODY></TABLE><BR>
<P><A name=N10193><SPAN class=atitle>小结 </SPAN></A></P>
<P>JSON 已经是 JavaScript 标准的一部分。目前,主流的浏览器对 JSON 支持都非常完善。应用 JSON,我们可以从
XML 的解析中摆脱出来,对那些应用 Ajax 的 Web 2.0 网站来说,JSON 确实是目前最灵活的轻量级方案。
</P><BR><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><IMG height=1 alt="" src="JSON 入门指南.files/blue_rule.gif"
width="100%"><BR><IMG height=6 alt=""
src="JSON 入门指南.files/c.gif" width=8
border=0></TD></TR></TBODY></TABLE>
<TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD><IMG height=4 alt="" src="JSON 入门指南.files/c.gif"
width="100%"><BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt=""
src="JSON 入门指南.files/u_bold.gif" width=16
border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox
href="http://www.ibm.com/developerworks/cn/web/wa-lo-json/#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
<P><SPAN class=atitle><A name=download>下载</A></SPAN></P>
<TABLE class=data-table-1 cellSpacing=0 cellPadding=0 width="100%"
border=0>
<TBODY>
<TR>
<TH scope=col>描述</TH>
<TH scope=col>名字</TH>
<TH scope=col>大小</TH>
<TH scope=col>下载方法</TH></TR>
<TR>
<TH class=tb-row scope=row>本文完整的源代码</TH>
<TD noWrap>jeedev-util.zip</TD>
<TD noWrap>90 KB</TD>
<TD noWrap><A class=fbox
href="http://javaeedev.googlecode.com/files/jeedev-util.zip"><B>HTTP</B></A></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR vAlign=top>
<TD colSpan=5><IMG height=12 alt=""
src="JSON 入门指南.files/c.gif" width=12 border=0></TD></TR>
<TR>
<TD><IMG height=16 alt="" src="JSON 入门指南.files/fw.gif"
width=16></TD>
<TD><A class=fbox
href="http://www.ibm.com/developerworks/cn/whichmethod.html">关于下载方法的信息</A></TD>
<TD><IMG height=1 alt="" src="JSON 入门指南.files/c.gif"
width=50></TD></TR></TBODY></TABLE><BR><BR>
<P><A name=resources><SPAN class=atitle>参考资料
</SPAN></A></P><B>学习</B><BR>
<UL>
<LI><A
href="http://www.ibm.com/developerworks/cn/web/wa-ajaxintro/">掌握
Ajax</A>:阅读本系列以前的文章。<BR><BR>
<LI><A href="http://www.json.org/">JSON.org</A>:访问 JSON Web
站点来进一步了解这种数据格式,包括到几种 JSON API 实现的链接。<BR><BR>
<LI>“<A
href="http://www.ibm.com/developerworks/cn/web/wa-ajaxintro10/">掌握
Ajax,第 10 部分: 使用 JSON 进行数据传输</A>”(developerWorks,2007 年 4 月):了解如何将
JavaScript 对象转变成 JSON 格式。<BR><BR>
<LI>“<A
href="http://www.ibm.com/developerworks/cn/web/wa-ajaxintro11.html">掌握
Ajax,第 11 部分: 服务器端的 JSON</A>”(developerWorks,2007 年 8 月):学习如何处理以
JSON 格式发送到服务器的数据以及如何使用相同格式对脚本进行回复。<BR><BR>
<LI>“<A
href="http://www.ibm.com/developerworks/cn/xml/x-xml2json/">从 XML
生成可与 Ajax 共同使用的 JSON</A>”(developerWorks,2007 年 6 月):通过本文,发掘使用
XSLT V2 从 XML 数据生成 JSON 的几种不同方法。<BR><BR>
<LI>“<A
href="http://www.ibm.com/developerworks/cn/xml/x-xml2jsonphp/">用
PHP 将 XML 转换成 JSON</A>”(developerWorks,2007 年 6 月):了解基于 PHP
的服务器程序,在将企业应用程序数据发送给浏览器应用程序之前,如何将其从 XML 格式转换成 JSON 格式。<BR><BR>
<LI><A
href="http://www.ibm.com/developerworks/cn/ajax/">developerWorks
Ajax 技术资源中心 </A>:能找到更多关于 Ajax 技术的文章和教程。 <BR><BR>
<LI><A
href="http://www.ibm.com/developerworks/cn/web/">developerWorks
Web 开发技术专区 </A>:提供了关于 Web 开发和架构方面的大量文章。 <BR><BR>
<LI>浏览 <A
href="http://www.ibm.com/developerworks/apps/SendTo?bookstore=safari">技术书店
</A>,查阅有关本文所述主题以及其他技术主题的书籍。 </LI></UL><BR><B>讨论</B><BR>
<UL>
<LI>查看 <A
href="http://www.ibm.com/developerworks/blogs/?S_TACT=105AGX52&S_CMP=cn-a-j">developerWorks
blogs</A>,加入 <A
href="http://www.ibm.com/developerworks/community?S_TACT=105AGX52&S_CMP=cn-a-j">developerWorks
社区 </A>。 <BR><BR></LI></UL><BR><BR>
<P><A name=author><SPAN class=atitle>关于作者</SPAN></A></P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD colSpan=3><IMG height=5 alt="" src="JSON 入门指南.files/c.gif"
width="100%"></TD></TR>
<TR vAlign=top align=left>
<TD>
<P></P></TD>
<TD><IMG height=5 alt="" src="JSON 入门指南.files/c.gif"
width=4></TD>
<TD width="100%">
<P>廖雪峰,长期从事 J2EE/J2ME 开发,对 Open Source 框架有深入研究。目前廖雪峰创建了 JavaEE
开发网(<A href="http://www.javaeedev.com/"
target=_blank>http://www.javaeedev.com/</A>),著有《<A
href="http://www.livebookstore.net/" target=_blank>Spring 2.0
核心技术与最佳实践</A>》一书。</P></TD></TR></TBODY></TABLE><BR><BR><BR>
<P class=no-print><SPAN class=atitle><A
name=rate>对本文的评价</A></SPAN></P><SPAN class=no-print>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD>
<FORM
action=https://www.ibm.com/developerworks/secure/cnratings.jsp
method=get><INPUT type=hidden value="JSON 入门指南 "
name=ArticleTitle><INPUT type=hidden value="Web development"
name=Zone><INPUT type=hidden
value=http://www.ibm.com/developerworks/cn/thankyou/
name=RedirectURL><INPUT type=hidden value=china
name=localsite>
<SCRIPT language=javascript>document.write('<input type="hidden" name="url" value="'+location.href+'" />');</SCRIPT>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD><IMG height=8 alt="" src="JSON 入门指南.files/c.gif"
width=100 border=0></TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=1 name=Rating>太差! (1)</TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=2 name=Rating>需提高 (2)</TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=3 name=Rating>一般;尚可
(3)</TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=4 name=Rating>好文章 (4)</TD></TR>
<TR vAlign=top>
<TD><INPUT type=radio value=5
name=Rating>真棒!(5)</TD></TR></TBODY></TABLE><BR><B>建议?</B><BR><TEXTAREA id=Comments name=Comments rows=5 wrap=virtual cols=60> </TEXTAREA><BR><BR><INPUT type=submit value=反馈意见></FORM></TD></TR>
<TR vAlign=top>
<TD bgColor=#ffffff><IMG height=8 alt=""
src="JSON 入门指南.files/c.gif" width=100
border=0></TD></TR></TBODY></TABLE></SPAN><BR>
<P><!-- --></P><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><BR><!--END RESERVED FOR FUTURE USE INCLUDE FILES--><BR><SPAN
class=no-print>
<TABLE cellSpacing=0 cellPadding=0 align=right>
<TBODY>
<TR align=right>
<TD><IMG height=8 alt="" src="JSON 入门指南.files/c.gif"
width="100%"><BR>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=center><IMG height=16 alt=""
src="JSON 入门指南.files/u_bold.gif" width=16
border=0><BR></TD>
<TD vAlign=top align=right><A class=fbox
href="http://www.ibm.com/developerworks/cn/web/wa-lo-json/#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR></SPAN></TD>
<TD width=10><IMG height=1 alt="" src="JSON 入门指南.files/c.gif"
width=10></TD></TR></TBODY></TABLE><SPAN class=small>IBM 公司保留在
developerWorks 网站上发表的内容的著作权。未经IBM公司或原始作者的书面明确许可,请勿转载。如果您希望转载,请通过 <A
href="https://www.ibm.com/developerworks/secure/reprintreq.jsp?domain=dwchina">提交转载请求表单</A>
联系我们的编辑团队。</SPAN></TD></TR></TBODY></TABLE><!--FOOTER_BEGIN--><!-- IBM FOOTER -->
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=bbg height=19>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD><SPAN class=spacer> </SPAN><A
class=mainlink href="http://www.ibm.com/cn/ibm/index.shtml">关于
IBM</A></TD>
<TD class=footer-divider width=27> </TD>
<TD><A class=mainlink
href="http://www.ibm.com/cn/ibm/privacy/index.shtml">隐私条约</A></TD>
<TD class=footer-divider width=27> </TD>
<TD><A class=mainlink href="http://www.ibm.com/contact/cn/">联系
IBM</A></TD>
<TD class=footer-divider width=27> </TD>
<TD><A class=mainlink
href="http://www.ibm.com/legal/cn/zh/">使用条款</A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!-- end footer -->
<SCRIPT language=JavaScript1.2 src="JSON 入门指南.files/stats.js"
type=text/javascript></SCRIPT>
<NOSCRIPT><IMG height=1 alt="" src="d:\我的文档\桌面\JSON 入门指南.files\c(1).gif" width=1
border=0></NOSCRIPT><!--FOOTER_END--><!--XSLT stylesheet used to transform this file: dw-document-html-5.10.xsl--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -