<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/xml-and-json-convert-helper-html/</link>
<title><![CDATA[xml 和 json 转换工具]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[编程杂谈]]></category>
<pubDate>Sat, 06 Nov 2010 11:08:08 +0000</pubDate> 
<guid>https://www.heckjj.com/xml-and-json-convert-helper-html/</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-family: 微软雅黑;">前几天在书上看的,实验了一下，结果还不错，收藏下来：<br/>package com;<br/>import java.util.*;<br/><br/>import net.sf.json.JSONObject;<br/><br/>import com.thoughtworks.xstream.XStream;<br/>import com.thoughtworks.xstream.io.xml.DomDriver;<br/><br/>public class ExtHelper &#123;<br/>&nbsp;&nbsp;public static String getXmlFormList(long recordTotal,List beanList)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;Total total = new Total(); //保存记录总数<br/>&nbsp;&nbsp;&nbsp;&nbsp;total.setResults(recordTotal); //设置记录总数到 total对象<br/>&nbsp;&nbsp;&nbsp;&nbsp;List results = new ArrayList(); //创建临时的 List 对象 results<br/>&nbsp;&nbsp;&nbsp;&nbsp;results.add(total); // 添加对象<br/>&nbsp;&nbsp;&nbsp;&nbsp;results.addAll(beanList); // 追加对象<br/>&nbsp;&nbsp;&nbsp;&nbsp;XStream sm = new XStream(new DomDriver()); //创建Xstream对象<br/>&nbsp;&nbsp;&nbsp;&nbsp;for(int i=0;i&lt;results.size();i++)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class c = results.get(i).getClass();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String b = c.getName();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String[] temp = b.split(&quot;&#92;&#92;.&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sm.alias(temp[temp.length-1],c);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;String xml = &quot;&lt;?xml version=&#039;1.0&#039; encoding=&#039;UTF-8&#039;?&gt;&quot;+sm.toXML(results);<br/>&nbsp;&nbsp;&nbsp;&nbsp;return xml;<br/>&nbsp;&nbsp;&#125;</span><span style="font-family: 微软雅黑;"><br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;public static String getJsonFromList(long recordTotal,List beanList)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;TotalJson total = new TotalJson();<br/>&nbsp;&nbsp;&nbsp;&nbsp;total.setResults(recordTotal);<br/>&nbsp;&nbsp;&nbsp;&nbsp;total.setItems(beanList);<br/>&nbsp;&nbsp;&nbsp;&nbsp;JSONObject jsonArray = JSONObject.fromObject(total);<br/>&nbsp;&nbsp;&nbsp;&nbsp;return jsonArray.toString();<br/>&nbsp;&nbsp;&#125;<br/>&#125;</span><br/><textarea name="code" class="java" rows="15" cols="100">package com;

import java.util.ArrayList;
import java.util.List;
import com.ExtHelper;

public class testjson &#123;
&nbsp;&nbsp;public static void main(String[] args)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;PhoneNumber homePhone = new PhoneNumber(&quot;宅电&quot;,&quot;123456&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;PhoneNumber officePhone = new PhoneNumber(&quot;办公电话&quot;,&quot;456789&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;List phoneList = new ArrayList();
&nbsp;&nbsp;&nbsp;&nbsp;phoneList.add(homePhone);
&nbsp;&nbsp;&nbsp;&nbsp;phoneList.add(officePhone);
&nbsp;&nbsp;&nbsp;&nbsp;String json = ExtHelper.getJsonFromList(phoneList.size(), phoneList);
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(json);
&nbsp;&nbsp;&#125;
&#125;</textarea><br/><textarea name="code" class="java" rows="15" cols="100">package com;

import java.util.ArrayList;
import java.util.List;

public class JsonListTest &#123;
&nbsp;&nbsp;public static void main(String[] args)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;PhoneNumber homePhone = new PhoneNumber(&quot;宅电&quot;,&quot;123456&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;PhoneNumber officePhone = new PhoneNumber(&quot;办公电话&quot;,&quot;456789&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;List phoneList&nbsp;&nbsp;= new ArrayList();
&nbsp;&nbsp;&nbsp;&nbsp;phoneList.add(homePhone);
&nbsp;&nbsp;&nbsp;&nbsp;phoneList.add(officePhone);
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&#125;
&#125;</textarea><br/>下面提供的是所需要的jar包下载：<br/><a href="attachment.php?fid=87">点击这里下载文件</a><br/>Tags - <a href="https://www.heckjj.com/tags/xml/" rel="tag">xml</a> , <a href="https://www.heckjj.com/tags/json/" rel="tag">json</a> , <a href="https://www.heckjj.com/tags/%25E8%25BD%25AC%25E6%258D%25A2/" rel="tag">转换</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/xml-and-json-convert-helper-html/#blogcomment</link>
<title><![CDATA[[评论] xml 和 json 转换工具]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/xml-and-json-convert-helper-html/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>