Jackson 框架使用教程,轻易转换JSON
fmms
13年前
<div id="cnblogs_post_body"> <p>Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象。</p> <p>前面有介绍过json-lib这个框架,在线博文:<span style="text-decoration:underline;">http://www.open-open.com/lib/view/open1327991992093.html</span><a href="http://www.open-open.com/lib/view/open1327991992093.html"></a></p> <p>相比json-lib框架,Jackson所依赖的jar包较少,简单易用并且性能也要相对高些。而且Jackson社区相对比较活跃,更新速度也比较快。</p> <p><b>一、</b><b>准备工作</b></p> <p>1、 下载依赖库jar包</p> <p>Jackson的jar all下载地址:<a href="/misc/goto?guid=4959517670998359922">http://jackson.codehaus.org/1.7.6/jackson-all-1.7.6.jar</a></p> <p>然后在工程中导入这个jar包即可开始工作</p> <p>官方示例:<a href="/misc/goto?guid=4959517671081565502">http://wiki.fasterxml.com/JacksonInFiveMinutes</a></p> <p>因为下面的程序是用junit测试用例运行的,所以还得添加junit的jar包。版本是junit-4.2.8</p> <p>如果你需要转换xml,那么还需要stax2-api.jar</p> <p>2、 测试类基本代码如下</p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">package</span> com.hoo.test;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> java.io.IOException;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> java.io.StringWriter;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> java.util.ArrayList;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> java.util.HashMap;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> java.util.Iterator;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> java.util.LinkedHashMap;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> java.util.List;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> java.util.Map;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> java.util.Set;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> org.codehaus.jackson.JsonEncoding;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> org.codehaus.jackson.JsonGenerationException;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> org.codehaus.jackson.JsonGenerator;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> org.codehaus.jackson.JsonParseException;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> org.codehaus.jackson.map.JsonMappingException;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> org.codehaus.jackson.map.ObjectMapper;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> org.codehaus.jackson.node.JsonNodeFactory;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> org.codehaus.jackson.xml.XmlMapper;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> org.junit.After;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> org.junit.Before;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> org.junit.Test;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">import</span> com.hoo.entity.AccountBean;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;">/**</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * <b>function:</b>Jackson 将java对象转换成JSON字符串,也可以将JSON字符串转换成java对象</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * jar-lib-version: jackson-all-1.6.2</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * jettison-1.0.1</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @author hoojo</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @createDate 2010-11-23 下午04:54:53</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @file JacksonTest.java</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @package com.hoo.test</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @project Spring3</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @blog http://blog.csdn.net/IBM_hoojo</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @email hoojo_@126.com</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @version 1.0</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> */</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">@SuppressWarnings(<span style="color:#006080;">"unchecked"</span>)</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> JacksonTest {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">private</span> JsonGenerator jsonGenerator = null;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">private</span> ObjectMapper objectMapper = null;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">private</span> AccountBean bean = null;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> @Before</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> init() {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean = <span style="color:#0000ff;">new</span> AccountBean();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setAddress(<span style="color:#006080;">"china-Guangzhou"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setEmail(<span style="color:#006080;">"hoojo_@126.com"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setId(1);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setName(<span style="color:#006080;">"hoojo"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> objectMapper = <span style="color:#0000ff;">new</span> ObjectMapper();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">try</span> {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (IOException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> @After</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> destory() {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">try</span> {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">if</span> (jsonGenerator != null) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.flush();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">if</span> (!jsonGenerator.isClosed()) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.close();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator = null;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> objectMapper = null;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean = null;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.gc();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (IOException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">}</pre> </div> </div> <p>3、 所需要的JavaEntity </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">package</span> com.hoo.entity;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> AccountBean {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">int</span> id;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">private</span> String name;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">private</span> String email;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">private</span> String address;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">private</span> Birthday birthday;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//getter、setter</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> @Override</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">public</span> String toString() {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">this</span>.name + <span style="color:#006080;">"#"</span> + <span style="color:#0000ff;">this</span>.id + <span style="color:#006080;">"#"</span> + <span style="color:#0000ff;">this</span>.address + <span style="color:#006080;">"#"</span> + <span style="color:#0000ff;">this</span>.birthday + <span style="color:#006080;">"#"</span> + <span style="color:#0000ff;">this</span>.email;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">}</pre> </div> </div> <p>Birthday </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">package</span> com.hoo.entity;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> Birthday {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">private</span> String birthday;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">public</span> Birthday(String birthday) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">super</span>();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">this</span>.birthday = birthday;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//getter、setter</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">public</span> Birthday() {}</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> @Override</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">public</span> String toString() {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">this</span>.birthday;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">}</pre> </div> </div> <p><b>二、</b><b>Java</b><b>对象转换成</b><b>JSON</b></p> <p>1、 JavaBean(Entity/Model)转换成JSON </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;">/**</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * <b>function:</b>将java对象转换成json字符串</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @author hoojo</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @createDate 2010-11-23 下午06:01:10</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> */</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">@Test</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> writeEntityJSON() {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">try</span> {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(<span style="color:#006080;">"jsonGenerator"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//writeObject可以转换java对象,eg:JavaBean/Map/List/Array等</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeObject(bean); </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(<span style="color:#006080;">"ObjectMapper"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//writeValue具有和writeObject相同的功能</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> objectMapper.writeValue(System.out, bean);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (IOException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">}</pre> </div> </div> <p>运行后结果如下: </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">jsonGenerator</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">{<span style="color:#006080;">"address"</span>:<span style="color:#006080;">"china-Guangzhou"</span>,<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"hoojo"</span>,<span style="color:#006080;">"id"</span>:1,<span style="color:#006080;">"birthday"</span>:null,<span style="color:#006080;">"email"</span>:<span style="color:#006080;">"hoojo_@126.com"</span>}</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">ObjectMapper</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">{<span style="color:#006080;">"address"</span>:<span style="color:#006080;">"china-Guangzhou"</span>,<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"hoojo"</span>,<span style="color:#006080;">"id"</span>:1,<span style="color:#006080;">"birthday"</span>:null,<span style="color:#006080;">"email"</span>:<span style="color:#006080;">"hoojo_@126.com"</span>}</pre> </div> </div> <p>上面分别利用JsonGenerator的writeObject方法和ObjectMapper的writeValue方法完成对Java对象的转换,二者传递的参数及构造的方式不同;JsonGenerator的创建依赖于ObjectMapper对象。也就是说如果你要使用 JsonGenerator来转换JSON,那么你必须创建一个ObjectMapper。但是你用ObjectMapper来转换JSON,则不需要 JSONGenerator。</p> <p>objectMapper的writeValue方法可以将一个Java对象转换成JSON。这个方法的参数一,需要提供一个输出流,转换后可以通过这个流来输出转换后的内容。或是提供一个File,将转换后的内容写入到File中。当然,这个参数也可以接收一个JSONGenerator,然后通过JSONGenerator来输出转换后的信息。第二个参数是将要被转换的Java对象。如果用三个参数的方法,那么是一个Config。这个 config可以提供一些转换时的规则,过指定的Java对象的某些属性进行过滤或转换等。</p> <p>2、 将Map集合转换成Json字符串 </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;">/**</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * <b>function:</b>将map转换成json字符串</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @author hoojo</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @createDate 2010-11-23 下午06:05:26</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> */</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">@Test</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> writeMapJSON() {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">try</span> {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> Map<String, Object> map = <span style="color:#0000ff;">new</span> HashMap<String, Object>();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> map.put(<span style="color:#006080;">"name"</span>, bean.getName());</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> map.put(<span style="color:#006080;">"account"</span>, bean);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean = <span style="color:#0000ff;">new</span> AccountBean();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setAddress(<span style="color:#006080;">"china-Beijin"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setEmail(<span style="color:#006080;">"hoojo@qq.com"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> map.put(<span style="color:#006080;">"account2"</span>, bean);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(<span style="color:#006080;">"jsonGenerator"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeObject(map);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(<span style="color:#006080;">""</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(<span style="color:#006080;">"objectMapper"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> objectMapper.writeValue(System.out, map);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (IOException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">}</pre> </div> </div> <p>转换后结果如下: </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">jsonGenerator</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">{<span style="color:#006080;">"account2"</span>:{<span style="color:#006080;">"address"</span>:<span style="color:#006080;">"china-Beijin"</span>,<span style="color:#006080;">"name"</span>:null,<span style="color:#006080;">"id"</span>:0,<span style="color:#006080;">"birthday"</span>:null,<span style="color:#006080;">"email"</span>:<span style="color:#006080;">"hoojo@qq.com"</span>},<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"hoojo"</span>,</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#006080;">"account"</span>:{<span style="color:#006080;">"address"</span>:<span style="color:#006080;">"china-Guangzhou"</span>,<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"hoojo"</span>,<span style="color:#006080;">"id"</span>:1,<span style="color:#006080;">"birthday"</span>:null,<span style="color:#006080;">"email"</span>:<span style="color:#006080;">"hoojo_@126.com"</span>}}</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">objectMapper</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">{<span style="color:#006080;">"account2"</span>:{<span style="color:#006080;">"address"</span>:<span style="color:#006080;">"china-Beijin"</span>,<span style="color:#006080;">"name"</span>:null,<span style="color:#006080;">"id"</span>:0,<span style="color:#006080;">"birthday"</span>:null,<span style="color:#006080;">"email"</span>:<span style="color:#006080;">"hoojo@qq.com"</span>},<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"hoojo"</span>,</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#006080;">"account"</span>:{<span style="color:#006080;">"address"</span>:<span style="color:#006080;">"china-Guangzhou"</span>,<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"hoojo"</span>,<span style="color:#006080;">"id"</span>:1,<span style="color:#006080;">"birthday"</span>:null,<span style="color:#006080;">"email"</span>:<span style="color:#006080;">"hoojo_@126.com"</span>}}</pre> </div> </div> <p>3、 将List集合转换成json </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;">/**</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * <b>function:</b>将list集合转换成json字符串</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @author hoojo</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @createDate 2010-11-23 下午06:05:59</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> */</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">@Test</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> writeListJSON() {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">try</span> {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> List<AccountBean> list = <span style="color:#0000ff;">new</span> ArrayList<AccountBean>();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> list.add(bean);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean = <span style="color:#0000ff;">new</span> AccountBean();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setId(2);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setAddress(<span style="color:#006080;">"address2"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setEmail(<span style="color:#006080;">"email2"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setName(<span style="color:#006080;">"haha2"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> list.add(bean);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(<span style="color:#006080;">"jsonGenerator"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//list转换成JSON字符串</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeObject(list);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(<span style="color:#006080;">"ObjectMapper"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//用objectMapper直接返回list转换成的JSON字符串</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(<span style="color:#006080;">"1###"</span> + objectMapper.writeValueAsString(list));</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.print(<span style="color:#006080;">"2###"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//objectMapper list转换成JSON字符串</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> objectMapper.writeValue(System.out, list);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (IOException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">}</pre> </div> </div> <p>结果如下: </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">jsonGenerator</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">[{<span style="color:#006080;">"address"</span>:<span style="color:#006080;">"china-Guangzhou"</span>,<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"hoojo"</span>,<span style="color:#006080;">"id"</span>:1,<span style="color:#006080;">"birthday"</span>:null,<span style="color:#006080;">"email"</span>:<span style="color:#006080;">"hoojo_@126.com"</span>},</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">{<span style="color:#006080;">"address"</span>:<span style="color:#006080;">"address2"</span>,<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"haha2"</span>,<span style="color:#006080;">"id"</span>:2,<span style="color:#006080;">"birthday"</span>:null,<span style="color:#006080;">"email"</span>:<span style="color:#006080;">"email2"</span>}]</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">ObjectMapper</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">1###[{<span style="color:#006080;">"address"</span>:<span style="color:#006080;">"china-Guangzhou"</span>,<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"hoojo"</span>,<span style="color:#006080;">"id"</span>:1,<span style="color:#006080;">"birthday"</span>:null,<span style="color:#006080;">"email"</span>:<span style="color:#006080;">"hoojo_@126.com"</span>},</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">{<span style="color:#006080;">"address"</span>:<span style="color:#006080;">"address2"</span>,<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"haha2"</span>,<span style="color:#006080;">"id"</span>:2,<span style="color:#006080;">"birthday"</span>:null,<span style="color:#006080;">"email"</span>:<span style="color:#006080;">"email2"</span>}]</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">2###[{<span style="color:#006080;">"address"</span>:<span style="color:#006080;">"china-Guangzhou"</span>,<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"hoojo"</span>,<span style="color:#006080;">"id"</span>:1,<span style="color:#006080;">"birthday"</span>:null,<span style="color:#006080;">"email"</span>:<span style="color:#006080;">"hoojo_@126.com"</span>},</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">{<span style="color:#006080;">"address"</span>:<span style="color:#006080;">"address2"</span>,<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"haha2"</span>,<span style="color:#006080;">"id"</span>:2,<span style="color:#006080;">"birthday"</span>:null,<span style="color:#006080;">"email"</span>:<span style="color:#006080;">"email2"</span>}]</pre> </div> </div> <p>外面就是多了个[]中括号;同样Array也可以转换,转换的JSON和上面的结果是一样的,这里就不再转换了。~.~</p> <p>4、下面来看看jackson提供的一些类型,用这些类型完成json转换;如果你使用这些类型转换JSON的话,那么你即使没有 JavaBean(Entity)也可以完成复杂的Java类型的JSON转换。下面用到这些类型构建一个复杂的Java对象,并完成JSON转换。 </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">@Test</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> writeOthersJSON() {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">try</span> {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> String[] arr = { <span style="color:#006080;">"a"</span>, <span style="color:#006080;">"b"</span>, <span style="color:#006080;">"c"</span> };</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(<span style="color:#006080;">"jsonGenerator"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> String str = <span style="color:#006080;">"hello world jackson!"</span>;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//byte</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeBinary(str.getBytes());</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//boolean</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeBoolean(true);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//null</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeNull();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//float</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeNumber(2.2f);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//char</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeRaw(<span style="color:#006080;">"c"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//String</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeRaw(str, 5, 10);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//String</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeRawValue(str, 5, 5);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//String</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeString(str);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeTree(JsonNodeFactory.instance.POJONode(str));</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//Object</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeStartObject();<span style="color:#008000;">//{</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeObjectFieldStart(<span style="color:#006080;">"user"</span>);<span style="color:#008000;">//user:{</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeStringField(<span style="color:#006080;">"name"</span>, <span style="color:#006080;">"jackson"</span>);<span style="color:#008000;">//name:jackson</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeBooleanField(<span style="color:#006080;">"sex"</span>, true);<span style="color:#008000;">//sex:true</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeNumberField(<span style="color:#006080;">"age"</span>, 22);<span style="color:#008000;">//age:22</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeEndObject();<span style="color:#008000;">//}</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeArrayFieldStart(<span style="color:#006080;">"infos"</span>);<span style="color:#008000;">//infos:[</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeNumber(22);<span style="color:#008000;">//22</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeString(<span style="color:#006080;">"this is array"</span>);<span style="color:#008000;">//this is array</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeEndArray();<span style="color:#008000;">//]</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeEndObject();<span style="color:#008000;">//}</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> AccountBean bean = <span style="color:#0000ff;">new</span> AccountBean();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setAddress(<span style="color:#006080;">"address"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setEmail(<span style="color:#006080;">"email"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setId(1);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> bean.setName(<span style="color:#006080;">"haha"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//complex Object</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeStartObject();<span style="color:#008000;">//{</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeObjectField(<span style="color:#006080;">"user"</span>, bean);<span style="color:#008000;">//user:{bean}</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeObjectField(<span style="color:#006080;">"infos"</span>, arr);<span style="color:#008000;">//infos:[array]</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> jsonGenerator.writeEndObject();<span style="color:#008000;">//}</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (Exception e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">}</pre> </div> </div> <p>运行后,结果如下: </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">jsonGenerator</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#006080;">"aGVsbG8gd29ybGQgamFja3NvbiE="</span> true null 2.2c world jac worl <span style="color:#006080;">"hello world jackson!"</span> <span style="color:#006080;">"hello world jackson!"</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> {<span style="color:#006080;">"user"</span>:{<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"jackson"</span>,<span style="color:#006080;">"sex"</span>:true,<span style="color:#006080;">"age"</span>:22},<span style="color:#006080;">"infos"</span>:[22,<span style="color:#006080;">"this is array"</span>]} </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">{<span style="color:#006080;">"user"</span>:{<span style="color:#006080;">"address"</span>:<span style="color:#006080;">"address"</span>,<span style="color:#006080;">"name"</span>:<span style="color:#006080;">"haha"</span>,<span style="color:#006080;">"id"</span>:1,<span style="color:#006080;">"birthday"</span>:null,<span style="color:#006080;">"email"</span>:<span style="color:#006080;">"email"</span>},<span style="color:#006080;">"infos"</span>:[<span style="color:#006080;">"a"</span>,<span style="color:#006080;">"b"</span>,<span style="color:#006080;">"c"</span>]}</pre> </div> </div> <p>怎么样?构造的json字符串和输出的结果是一致的吧。关键看懂用JSONGenerator提供的方法,完成一个Object的构建。</p> <p><b>三、</b><b>JSON</b><b>转换成</b><b>Java</b><b>对象</b></p> <p>1、 将json字符串转换成JavaBean对象 </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">@Test</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> readJson2Entity() {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> String json = <span style="color:#006080;">"{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}"</span>;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">try</span> {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> AccountBean acc = objectMapper.readValue(json, AccountBean.<span style="color:#0000ff;">class</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(acc.getName());</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(acc);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (JsonParseException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (JsonMappingException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (IOException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">}</pre> </div> </div> <p>很简单,用到了ObjectMapper这个对象的readValue这个方法,这个方法需要提供2个参数。第一个参数就是解析的JSON字符串,第二个参数是即将将这个JSON解析吃什么Java对象,Java对象的类型。当然,还有其他相同签名方法,如果你有兴趣可以一一尝试使用方法,当然使用的方法和当前使用的方法大同小异。运行后,结果如下: </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">haha</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">haha#1#address#null#email</pre> </div> </div> <p>2、 将json字符串转换成List<Map>集合 </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;">/**</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * <b>function:</b>json字符串转换成list<map></span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @author hoojo</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @createDate 2010-11-23 下午06:12:01</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> */</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">@Test</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> readJson2List() {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> String json = <span style="color:#006080;">"[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"</span>+</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#006080;">"{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]"</span>;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">try</span> {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> List<LinkedHashMap<String, Object>> list = objectMapper.readValue(json, List.<span style="color:#0000ff;">class</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(list.size());</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">int</span> i = 0; i < list.size(); i++) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> Map<String, Object> map = list.get(i);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> Set<String> set = map.keySet();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">for</span> (Iterator<String> it = set.iterator();it.hasNext();) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> String key = it.next();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(key + <span style="color:#006080;">":"</span> + map.get(key));</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (JsonParseException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (JsonMappingException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (IOException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">}</pre> </div> </div> <p>尝试过将上面的JSON转换成List,然后List中存放AccountBean,但结果失败了。但是支持Map集合。因为你转成List.class,但是不知道List存放何种类型。只好默然Map类型。因为所有的对象都可以转换成Map结合,运行后结果如下: </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">2</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">address:address2</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">name:haha2</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">id:2</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">email:email2</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">address:address</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">name:haha</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">id:1</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">email:email</pre> </div> </div> <p>3、 Json字符串转换成Array数组,由于上面的泛型转换不能识别到集合中的对象类型。所有这里用对象数组,可以解决这个问题。只不过它不再是集合,而是一个数组。当然这个不重要,你可以用Arrays.asList将其转换成List即可。 </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;">/**</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * <b>function:</b>json字符串转换成Array</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @author hoojo</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @createDate 2010-11-23 下午06:14:01</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> */</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">@Test</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> readJson2Array() {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> String json = <span style="color:#006080;">"[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"</span>+</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#006080;">"{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]"</span>;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">try</span> {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> AccountBean[] arr = objectMapper.readValue(json, AccountBean[].<span style="color:#0000ff;">class</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(arr.length);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">int</span> i = 0; i < arr.length; i++) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(arr[i]);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (JsonParseException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (JsonMappingException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (IOException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">}</pre> </div> </div> <p>运行后的结果: </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">2</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">haha2#2#address2#null#email2</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">haha#1#address#null#email</pre> </div> </div> <p>4、 Json字符串转换成Map集合 </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;">/**</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * <b>function:</b>json字符串转换Map集合</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @author hoojo</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @createDate Nov 27, 2010 3:00:06 PM</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> */</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">@Test</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> readJson2Map() {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> String json = <span style="color:#006080;">"{\"success\":true,\"A\":{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"</span>+</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#006080;">"\"B\":{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}}"</span>;</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">try</span> {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> Map<String, Map<String, Object>> maps = objectMapper.readValue(json, Map.<span style="color:#0000ff;">class</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(maps.size());</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> Set<String> key = maps.keySet();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> Iterator<String> iter = key.iterator();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">while</span> (iter.hasNext()) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> String field = iter.next();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(field + <span style="color:#006080;">":"</span> + maps.get(field));</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (JsonParseException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (JsonMappingException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (IOException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">}</pre> </div> </div> <p>运行后结果如下: </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">3</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">success:true</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">A:{address=address2, name=haha2, id=2, email=email2}</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">B:{address=address, name=haha, id=1, email=email}</pre> </div> </div> <p><b>四、</b><b>Jackson</b><b>对</b><b>XML</b><b>的支持</b></p> <p>Jackson也可以完成java对象到xml的转换,转换后的结果要比json-lib更直观,不过它依赖于stax2-api.jar这个jar包。 </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;">/**</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * <b>function:</b>java对象转换成xml文档</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * 需要额外的jar包 stax2-api.jar</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @author hoojo</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> * @createDate 2010-11-23 下午06:11:21</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#008000;"> */</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">@Test</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> writeObject2Xml() {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//stax2-api-3.0.2.jar</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(<span style="color:#006080;">"XmlMapper"</span>);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> XmlMapper xml = <span style="color:#0000ff;">new</span> XmlMapper();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#0000ff;">try</span> {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//javaBean转换成xml</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//xml.writeValue(System.out, bean);</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> StringWriter sw = <span style="color:#0000ff;">new</span> StringWriter();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> xml.writeValue(sw, bean);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(sw.toString());</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//List转换成xml</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> List<AccountBean> list = <span style="color:#0000ff;">new</span> ArrayList<AccountBean>();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> list.add(bean);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> list.add(bean);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(xml.writeValueAsString(list));</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> </pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> <span style="color:#008000;">//Map转换xml文档</span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> Map<String, AccountBean> map = <span style="color:#0000ff;">new</span> HashMap<String, AccountBean>();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> map.put(<span style="color:#006080;">"A"</span>, bean);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> map.put(<span style="color:#006080;">"B"</span>, bean);</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> System.out.println(xml.writeValueAsString(map));</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (JsonGenerationException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (JsonMappingException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> } <span style="color:#0000ff;">catch</span> (IOException e) {</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> e.printStackTrace();</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"> }</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">}</pre> </div> </div> <p>运行上面的方法,结果如下: </p> <div id="codeSnippetWrapper"> <div style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;" id="codeSnippet"> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;">XmlMapper</pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;"><</span><span style="color:#800000;">unknown</span><span style="color:#0000ff;">><</span><span style="color:#800000;">address</span><span style="color:#0000ff;">></span>china-Guangzhou<span style="color:#0000ff;"></</span><span style="color:#800000;">address</span><span style="color:#0000ff;">><</span><span style="color:#800000;">name</span><span style="color:#0000ff;">></span>hoojo<span style="color:#0000ff;"></</span><span style="color:#800000;">name</span><span style="color:#0000ff;">><</span><span style="color:#800000;">id</span><span style="color:#0000ff;">></span>1<span style="color:#0000ff;"></</span><span style="color:#800000;">id</span><span style="color:#0000ff;">><</span><span style="color:#800000;">birthday</span><span style="color:#0000ff;">/><</span><span style="color:#800000;">email</span><span style="color:#0000ff;">></span>hoojo_@126.com<span style="color:#0000ff;"></</span><span style="color:#800000;">email</span><span style="color:#0000ff;">></</span><span style="color:#800000;">unknown</span><span style="color:#0000ff;">></span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;"><</span><span style="color:#800000;">unknown</span><span style="color:#0000ff;">><</span><span style="color:#800000;">unknown</span><span style="color:#0000ff;">><</span><span style="color:#800000;">address</span><span style="color:#0000ff;">></span>china-Guangzhou<span style="color:#0000ff;"></</span><span style="color:#800000;">address</span><span style="color:#0000ff;">><</span><span style="color:#800000;">name</span><span style="color:#0000ff;">></span>hoojo<span style="color:#0000ff;"></</span><span style="color:#800000;">name</span><span style="color:#0000ff;">><</span><span style="color:#800000;">id</span><span style="color:#0000ff;">></span>1<span style="color:#0000ff;"></</span><span style="color:#800000;">id</span><span style="color:#0000ff;">><</span><span style="color:#800000;">birthday</span><span style="color:#0000ff;">/><</span><span style="color:#800000;">email</span><span style="color:#0000ff;">></span>hoojo_@126.com<span style="color:#0000ff;"></</span><span style="color:#800000;">email</span><span style="color:#0000ff;">></</span><span style="color:#800000;">unknown</span><span style="color:#0000ff;">></span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;"><</span><span style="color:#800000;">email</span><span style="color:#0000ff;">><</span><span style="color:#800000;">address</span><span style="color:#0000ff;">></span>china-Guangzhou<span style="color:#0000ff;"></</span><span style="color:#800000;">address</span><span style="color:#0000ff;">><</span><span style="color:#800000;">name</span><span style="color:#0000ff;">></span>hoojo<span style="color:#0000ff;"></</span><span style="color:#800000;">name</span><span style="color:#0000ff;">><</span><span style="color:#800000;">id</span><span style="color:#0000ff;">></span>1<span style="color:#0000ff;"></</span><span style="color:#800000;">id</span><span style="color:#0000ff;">><</span><span style="color:#800000;">birthday</span><span style="color:#0000ff;">/><</span><span style="color:#800000;">email</span><span style="color:#0000ff;">></span>hoojo_@126.com<span style="color:#0000ff;"></</span><span style="color:#800000;">email</span><span style="color:#0000ff;">></</span><span style="color:#800000;">email</span><span style="color:#0000ff;">></</span><span style="color:#800000;">unknown</span><span style="color:#0000ff;">></span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:white;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;"><</span><span style="color:#800000;">unknown</span><span style="color:#0000ff;">><</span><span style="color:#800000;">A</span><span style="color:#0000ff;">><</span><span style="color:#800000;">address</span><span style="color:#0000ff;">></span>china-Guangzhou<span style="color:#0000ff;"></</span><span style="color:#800000;">address</span><span style="color:#0000ff;">><</span><span style="color:#800000;">name</span><span style="color:#0000ff;">></span>hoojo<span style="color:#0000ff;"></</span><span style="color:#800000;">name</span><span style="color:#0000ff;">><</span><span style="color:#800000;">id</span><span style="color:#0000ff;">></span>1<span style="color:#0000ff;"></</span><span style="color:#800000;">id</span><span style="color:#0000ff;">><</span><span style="color:#800000;">birthday</span><span style="color:#0000ff;">/><</span><span style="color:#800000;">email</span><span style="color:#0000ff;">></span>hoojo_@126.com<span style="color:#0000ff;"></</span><span style="color:#800000;">email</span><span style="color:#0000ff;">></</span><span style="color:#800000;">A</span><span style="color:#0000ff;">></span></pre> <pre style="border-bottom-style:none;text-align:left;padding-bottom:0px;line-height:12pt;border-right-style:none;background-color:#f4f4f4;margin:0em;padding-left:0px;width:100%;padding-right:0px;font-family:'Courier New', courier, monospace;direction:ltr;border-top-style:none;color:black;font-size:10pt;border-left-style:none;overflow:visible;padding-top:0px;"><span style="color:#0000ff;"><</span><span style="color:#800000;">B</span><span style="color:#0000ff;">><</span><span style="color:#800000;">address</span><span style="color:#0000ff;">></span>china-Guangzhou<span style="color:#0000ff;"></</span><span style="color:#800000;">address</span><span style="color:#0000ff;">><</span><span style="color:#800000;">name</span><span style="color:#0000ff;">></span>hoojo<span style="color:#0000ff;"></</span><span style="color:#800000;">name</span><span style="color:#0000ff;">><</span><span style="color:#800000;">id</span><span style="color:#0000ff;">></span>1<span style="color:#0000ff;"></</span><span style="color:#800000;">id</span><span style="color:#0000ff;">><</span><span style="color:#800000;">birthday</span><span style="color:#0000ff;">/><</span><span style="color:#800000;">email</span><span style="color:#0000ff;">></span>hoojo_@126.com<span style="color:#0000ff;"></</span><span style="color:#800000;">email</span><span style="color:#0000ff;">></</span><span style="color:#800000;">B</span><span style="color:#0000ff;">></</span><span style="color:#800000;">unknown</span><span style="color:#0000ff;">></span></pre> </div> </div> <p>看结果,根节点都是unknown 这个问题还没有解决,由于根节点没有转换出来,所有导致解析xml到Java对象,也无法完成。<br /> <br /> 转自:<a href="/misc/goto?guid=4959517671165113118" target="_blank">http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html</a></p> </div>