﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-程序员成长史-随笔分类-json</title><link>http://www.blogjava.net/Good-Game/category/25648.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 08 Oct 2007 19:10:27 GMT</lastBuildDate><pubDate>Mon, 08 Oct 2007 19:10:27 GMT</pubDate><ttl>60</ttl><item><title>json 测试</title><link>http://www.blogjava.net/Good-Game/archive/2007/10/08/151071.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Mon, 08 Oct 2007 07:08:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/10/08/151071.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/151071.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/10/08/151071.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/151071.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/151071.html</trackback:ping><description><![CDATA[
		<b>
				<font color="#ffa500">参照：</font>
		</b>
		<br />    <font color="#0000ff">http://json-lib.sourceforge.net/usage.html</font><br /><br /><b><font color="#ffa500">先 json &lt;-&gt; object </font></b><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> test.json;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.ArrayList;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.HashMap;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.List;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.Map;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.beanutils.PropertyUtils;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> com.jjm.viewBean.reportTableBean;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> net.sf.json.JSONArray;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> net.sf.json.JSONFunction;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> net.sf.json.JSONObject;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> junit.framework.TestCase;<br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> JsonTest </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> TestCase {<br /><br /></span><b><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> <font color="#ffa500">object to json</font> ************************************************************</span></b><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> testList(){<br />        </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);">[] boolArray </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);">[]{</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">};   <br />            JSONArray jsonArray1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> JSONArray.fromObject( boolArray );   <br />            System.out.println( jsonArray1 );   <br />            </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> prints [true,false,true]  </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">            <br />            List list </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ArrayList();   <br />            list.add( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">first</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );   <br />            list.add( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">second</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );   <br />            JSONArray jsonArray2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> JSONArray.fromObject( list );   <br />            System.out.println( jsonArray2 );   <br />            </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> prints ["first","second"]  </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br />            JSONArray jsonArray3 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> JSONArray.fromObject( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">['json','is','easy']</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );   <br />            System.out.println( jsonArray3 );   <br />            </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> prints ["json","is","easy"]   </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    }<br />    <br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> testMap(){<br />        Map map </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> HashMap();   <br />         map.put( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">json</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );   <br />         map.put( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">bool</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, Boolean.TRUE );   <br />         <br />         map.put( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">int</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Integer(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">) );   <br />         map.put( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">arr</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> String[]{</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">b</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">} );   <br />         map.put( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">func</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">function(i){ return this.arr[i]; }</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );   <br />         JSONObject json </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> JSONObject.fromObject( map );   <br />         System.out.println( json );   <br />         </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">{"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"name":"json","bool":true}</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    }<br /><br />    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />     * Bean.java <br />        private String name = "json";   <br />        private int pojoId = 1;   <br />        private char[] options = new char[]{'a','f'};   <br />        private String func1 = "function(i){ return this.options[i]; }";   <br />        private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");<br />    </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> testBean(){<br />         JSONObject jsonObject </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> JSONObject.fromObject( </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> JsonBean() );   <br />         System.out.println( jsonObject );   <br />         </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">{"func1":function(i){ return this.options[i]; },"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){ return this.options[i]; }}  </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">     }<br />     <br />     </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />      * private int row ;<br />          private int col ;<br />          private String value ;<br />      *<br />      </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> testBeans(){<br />         List list </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> ArrayList();<br />         JsonBean2 jb1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> JsonBean2();<br />         jb1.setCol(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />         jb1.setRow(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br />         jb1.setValue(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">xx</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />         <br />         JsonBean2 jb2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> JsonBean2();<br />         jb2.setCol(</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">);<br />         jb2.setRow(</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">);<br />         jb2.setValue(</span><span style="color: rgb(0, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">);<br />         <br />         <br />         list.add(jb1);<br />         list.add(jb2);<br />         <br />         JSONArray ja </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> JSONArray.fromObject(list);<br />         System.out.println( ja.toString() );<br />         </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">     }<br />     <br />     <br /><br /></span><b><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">   <font color="#ffa500"> json  to object</font> ************************************************************     </span></b><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">     <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> testJsonBeanUtil()</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> Exception{<br />             String json </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">{name=\</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">json\</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;   <br />            JSONObject jsonObject </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> JSONObject.fromString(json);   <br />            Object bean </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> JSONObject.toBean( jsonObject );   <br />            assertEquals( jsonObject.get( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ), PropertyUtils.getProperty( bean, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ) );   <br />            assertEquals( jsonObject.get( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">bool</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ), PropertyUtils.getProperty( bean, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">bool</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ) );   <br />            assertEquals( jsonObject.get( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">int</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ), PropertyUtils.getProperty( bean, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">int</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ) );   <br />            assertEquals( jsonObject.get( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">double</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ), PropertyUtils.getProperty( bean, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">double</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ) );   <br />            assertEquals( jsonObject.get( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">func</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ), PropertyUtils.getProperty( bean, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">func</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ) );   <br />            List expected </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> JSONArray.toList( jsonObject.getJSONArray( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">array</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ) );   <br />            assertEquals( expected, (List) PropertyUtils.getProperty( bean, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">array</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ) );  <br />     }<br />     <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> testJsonBean(){<br />             String json </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">{\</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">value\</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:\</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">xx\</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,\</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">row\</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:1,\</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">col\</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:1}</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;   <br />            JSONObject jsonObject </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> JSONObject.fromString(json);<br />            JsonBean2 bean </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (JsonBean2) JSONObject.toBean( jsonObject, JsonBean2.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> );   <br />            assertEquals( jsonObject.get( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">col</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ),</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Integer( bean.getCol())  );   <br />            assertEquals( jsonObject.get( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">row</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ), </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Integer( bean.getRow() ) );   <br />            assertEquals( jsonObject.get( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">value</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ), bean.getValue() );  <br />     }<br />}                                                                                                                                                                             <br /></span></div><br /><br /><b><font color="#ffa500">json &lt;-&gt; xml</font></b><br /><font color="#ffa500">JSON to XML</font><table class="bodyTable"><tbody><tr class="b"><td><div class="dp-highlighter nogutter"><ol class="dp-j" start="1"><li class="alt"><span><span>JSONObject json = </span><span class="keyword">new</span><span> JSONObject( </span><span class="keyword">true</span><span> );  </span></span></li><li class=""><span>String xml = XMLSerializer.write( json );  </span></li></ol></div><textarea style="display: none;" cols="70" rows="2" class="java:nocontrols:nogutter" name="srccode">   JSONObject json = new JSONObject( true );
   String xml = XMLSerializer.write( json );</textarea></td><td><div class="dp-highlighter nogutter"><ol class="dp-xml" start="1"><li class="alt"><span><span class="tag">&lt;</span><span class="tag-name">o</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"object"</span><span> </span><span class="attribute">null</span><span>=</span><span class="attribute-value">"true"</span><span class="tag">&gt;</span><span>  </span></span></li><li class=""><span>      </span></li></ol></div><textarea style="display: none;" cols="70" rows="1" class="xml:nocontrols:nogutter" name="srccode">   &lt;o class="object" null="true"&gt;
       </textarea></td></tr><tr class="a"><td><div class="dp-highlighter nogutter"><ol class="dp-j" start="1"><li class="alt"><span><span>JSONObject json = JSONObject.fromObject(</span><span class="string">"{\"name\":\"json\",\"bool\":true,\"int\":1}"</span><span>);  </span></span></li><li class=""><span>String xml = XMLSerializer.write( json );  </span></li></ol></div><textarea style="display: none;" cols="70" rows="2" class="java:nocontrols:nogutter" name="srccode">   JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");
   String xml = XMLSerializer.write( json );</textarea></td><td><div class="dp-highlighter nogutter"><ol class="dp-xml" start="1"><li class="alt"><span><span class="tag">&lt;</span><span class="tag-name">o</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"object"</span><span class="tag">&gt;</span><span>  </span></span></li><li class=""><span>   <span class="tag">&lt;</span><span class="tag-name">name</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"string"</span><span class="tag">&gt;</span><span>json</span><span class="tag">&lt;/</span><span class="tag-name">name</span><span class="tag">&gt;</span><span>  </span></span></li><li class="alt"><span>   <span class="tag">&lt;</span><span class="tag-name">bool</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"boolean"</span><span class="tag">&gt;</span><span>true</span><span class="tag">&lt;/</span><span class="tag-name">bool</span><span class="tag">&gt;</span><span>  </span></span></li><li class=""><span>   <span class="tag">&lt;</span><span class="tag-name">int</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"number"</span><span class="tag">&gt;</span><span>1</span><span class="tag">&lt;/</span><span class="tag-name">int</span><span class="tag">&gt;</span><span>  </span></span></li><li class="alt"><span><span class="tag">&lt;/</span><span class="tag-name">o</span><span class="tag">&gt;</span><span>  </span></span></li></ol></div><textarea style="display: none;" cols="70" rows="5" class="xml:nocontrols:nogutter" name="srccode">   &lt;o class="object"&gt;
      &lt;name type="string"&gt;json&lt;/name&gt;
      &lt;bool type="boolean"&gt;true&lt;/bool&gt;
      &lt;int type="number"&gt;1&lt;/int&gt;
   &lt;/o&gt;
   </textarea></td></tr><tr class="b"><td><div class="dp-highlighter nogutter"><ol class="dp-j" start="1"><li class="alt"><span><span>JSONArray json = JSONArray.fromObject(</span><span class="string">"[1,2,3]"</span><span>);  </span></span></li><li class=""><span>String xml = XMLSerializer.write( json );  </span></li></ol></div><textarea style="display: none;" cols="70" rows="2" class="java:nocontrols:nogutter" name="srccode">   JSONArray json = JSONArray.fromObject("[1,2,3]");
   String xml = XMLSerializer.write( json );</textarea></td><td><div class="dp-highlighter nogutter"><ol class="dp-xml" start="1"><li class="alt"><span><span class="tag">&lt;</span><span class="tag-name">a</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"array"</span><span class="tag">&lt;</span><span>  </span></span></li><li class=""><span>   <span class="tag">&lt;</span><span class="tag-name">e</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"number"</span><span class="tag">&gt;</span><span>1</span><span class="tag">&lt;/</span><span class="tag-name">e</span><span class="tag">&gt;</span><span>  </span></span></li><li class="alt"><span>   <span class="tag">&lt;</span><span class="tag-name">e</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"number"</span><span class="tag">&gt;</span><span>2</span><span class="tag">&lt;/</span><span class="tag-name">e</span><span class="tag">&gt;</span><span>  </span></span></li><li class=""><span>   <span class="tag">&lt;</span><span class="tag-name">e</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"number"</span><span class="tag">&gt;</span><span>3</span><span class="tag">&lt;/</span><span class="tag-name">e</span><span class="tag">&gt;</span><span>  </span></span></li><li class="alt"><span><span class="tag">&lt;/</span><span class="tag-name">a</span><span class="tag">&gt;</span><span>  </span></span></li></ol></div></td></tr></tbody></table><font color="#ffa500"> xml to json </font><br /><ol class="dp-xml" start="1"><li class="alt"><span><span class="tag">&lt;</span><span class="tag-name">a</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"array"</span><span class="tag">&gt;</span><span>  </span></span></li><li class=""><span>  <span class="tag">&lt;</span><span class="tag-name">e</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"function"</span><span> </span><span class="attribute">params</span><span>=</span><span class="attribute-value">"i,j"</span><span class="tag">&gt;</span><span>  </span></span></li><li class="alt"><span>      return matrix[i][j];  </span></li><li class=""><span>  <span class="tag">&lt;/</span><span class="tag-name">e</span><span class="tag">&gt;</span><span>  </span></span></li><li class="alt"><span><span class="tag">&lt;/</span><span class="tag-name">a</span><span class="tag">&gt;</span><span>  </span></span></li></ol><textarea style="display: none;" cols="70" rows="5" class="xml:nocontrols:nogutter" name="srccode">   &lt;a class="array"&gt;
     &lt;e type="function" params="i,j"&gt;
         return matrix[i][j];
     &lt;/e&gt;
   &lt;/a&gt;
   </textarea><div class="dp-highlighter nogutter"><ol class="dp-j" start="1"><li class="alt"><span><span>JSONArray json = (JSONArray) XMLSerializer.read( xml );  </span></span></li><li class=""><span>System.out.println( json );  </span></li><li class="alt"><span><span class="comment">// prints [function(i,j){ return matrix[i][j]; }]</span><span>  <br /></span></span></li></ol></div><br /><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/151071.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-10-08 15:08 <a href="http://www.blogjava.net/Good-Game/archive/2007/10/08/151071.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>json 使用</title><link>http://www.blogjava.net/Good-Game/archive/2007/09/13/144785.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Thu, 13 Sep 2007 03:36:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/09/13/144785.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/144785.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/09/13/144785.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/144785.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/144785.html</trackback:ping><description><![CDATA[
		<p>
				<br />
		</p>
		<p>使用后好处 <br />    java 和 javascript 使用同统一对象,语法 . 数据从 sever 到 v层 和 v层 到 sever  方便.<br />            简单说就是 JBean 不做修改拿到V层 当 JSBean (javascript 类) 中用<br /><br />1.url 乱码问题参照 <a class="postTitle2" id="homepage1_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl00_TitleUrl" href="/Good-Game/archive/2007/09/13/144737.html">Ajax uri 乱码问题总结(IE,FF)</a><br />2.使用jar是 jxpath ; json  ......(与相关)<br />               jxpath 参照 <a class="entrylistItemTitle" id="CategoryEntryList1_EntryStoryList_Entries_ctl00_TitleUrl" href="/Good-Game/archive/2007/08/13/136318.html">jxpath 学习笔记</a> <br />               json 参照 <a href="http://blog.csdn.net/jawsy/archive/2007/04/09/1557165.aspx">使用json-lib</a><br /></p>
		<p>例题说明 <b><font color="#000080">数据 name,avg 在通过 ajax json 后 avg +1 再展现到页面</font></b><br />本页需要 json.js 下载到 <a href="http://www.json.org/json.js">http://www.json.org/json.js</a> </p>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);">
				<span style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 0);">&lt;%</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">@ page pageEncoding</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">GBK</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 0);">%&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 0);">&lt;%</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">@ page contentType</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">text/html; charset=GBK</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 0);">%&gt;</span>
				<span style="color: rgb(0, 0, 0);">  <br /></span>
				<span style="color: rgb(0, 0, 255);">&lt;</span>
				<span style="color: rgb(128, 0, 0);">html</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />  </span>
				<span style="color: rgb(0, 0, 255);">&lt;</span>
				<span style="color: rgb(128, 0, 0);">head</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">&lt;</span>
				<span style="color: rgb(128, 0, 0);">title</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">json.html</span>
				<span style="color: rgb(0, 0, 255);">&lt;/</span>
				<span style="color: rgb(128, 0, 0);">title</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    <br />    </span>
				<span style="color: rgb(0, 0, 255);">&lt;</span>
				<span style="color: rgb(128, 0, 0);">meta </span>
				<span style="color: rgb(255, 0, 0);">http-equiv</span>
				<span style="color: rgb(0, 0, 255);">="keywords"</span>
				<span style="color: rgb(255, 0, 0);"> content</span>
				<span style="color: rgb(0, 0, 255);">="keyword1,keyword2,keyword3"</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<strong>
						<span style="color: rgb(0, 0, 255);">&lt;</span>
						<span style="color: rgb(128, 0, 0);">SCRIPT </span>
						<span style="color: rgb(255, 0, 0);">src</span>
						<span style="color: rgb(0, 0, 255);">="../js/json.js"</span>
						<span style="color: rgb(255, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 255);">&gt;&lt;/</span>
						<span style="color: rgb(128, 0, 0);">SCRIPT</span>
						<span style="color: rgb(0, 0, 255);">&gt;</span>
				</strong>
				<span style="color: rgb(0, 0, 0);"> <br />    <br /></span>
				<span style="color: rgb(0, 0, 255);">&lt;</span>
				<span style="color: rgb(128, 0, 0);">script </span>
				<span style="color: rgb(255, 0, 0);">language</span>
				<span style="color: rgb(0, 0, 255);">="javascript"</span>
				<span style="color: rgb(255, 0, 0);"> type</span>
				<span style="color: rgb(0, 0, 255);">="text/javascript"</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">var</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> request </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">false</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">;<br />    <br /> <font color="#008000">  // javascript  Ajax  没什么好说的 这可以用 prototype </font><a class="titlelink" id="Editor_Results_rprSelectionList_ctl01_LinkTitle" href="/Good-Game/articles/115343.html"><font color="#0000ff">prototype.js 的理解</font></a><font color="#008000">  ,dojo </font><a href="?catid=25594"><font color="#0000ff">dojo</font></a><font color="#008000">  等 ajax<br />  //本例 为方便直接写了 ^_^</font> <br />    </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">function</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> getOpen(){      <br />       </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">try</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> {<br />         request </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">new</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> XMLHttpRequest();<br />       } </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">catch</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> (trymicrosoft) {<br />         </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">try</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> {<br />           request </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">new</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> ActiveXObject(</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">Msxml2.XMLHTTP</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">);<br />         } </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">catch</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> (othermicrosoft) {<br />           </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">try</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> {<br />             request </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">new</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> ActiveXObject(</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">Microsoft.XMLHTTP</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">);<br />           } </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">catch</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> (failed) {<br />             request </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">false</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">;<br />           }  <br />         }<br />       }<br />    }   <br />       <br />       <br />      </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">function</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> getCustomerInfo() {<br />      getOpen();<br />       </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">if</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> (</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">!</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">request)<br />         alert(</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">Error initializing XMLHttpRequest!</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">);<br />         <br /><font color="#008000">       //这通过 url 把数据传给 server <br />       //数据来源 javascript 类 就下方 DBdata</font>    <font color="#006400"> string: name:我名字^_^  ,  int: avg:年龄^_^ <br /></font>         </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">var</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> url </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">/json/json?jsonStr=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">+</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> (</span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">new</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> DBdata()).toJSONString() ;<br />         request.open(</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">GET</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">"</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">, url, </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">true</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">);<br />         request.send(</span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">null</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">);<br />         request.onreadystatechange </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> updatePage;<br />       }<br />       <br />       <font color="#008000">// ajax 处理 数据后返回的 结果 <br /></font>      <font color="#008000">// 年龄 +1 并输入到 输入框中</font><br />       </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">function</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> updatePage(){<br />       </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">if</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> (request.readyState </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">==</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">4</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">) <br />           </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">if</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> (request.status </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">==</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">200</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">){ <br />           <font color="#008000"> //得到 json str <br /></font>             </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">var</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> jss </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> request.responseText;<br />            <font color="#006400">//加载到 javascript 类中 <b>string -&gt; jsBean</b></font><br />             </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">var</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> jsobj </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> eval('(' </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">+</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> jss </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">+</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> ')');<br />             <font color="#006400"><strong>//使用和 java对象一样 ^_^</strong></font><br />             document.getElementById('xx').value </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> <font color="#ffa500">jsobj.name</font> </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">+</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">':'</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">+</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> <font color="#ffa500">jsobj.avg</font> ;<br />            }<br />       }<br />    <br /><font color="#008000">//数据来源</font><br />     </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">function</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> DBdata(){<br />         </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">this</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">.name </span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);"> '刘凯毅';<br />         </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">this</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">.avg</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">23</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">; <br />         </span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">this</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">.init</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">=</span>
				<span style="color: rgb(0, 0, 255); background-color: rgb(245, 245, 245);">function</span>
				<span style="color: rgb(0, 0, 0); background-color: rgb(245, 245, 245);">(){<br />             alert('呵呵');<br />         }<br />     }<br /></span>
				<span style="color: rgb(0, 0, 255);">&lt;/</span>
				<span style="color: rgb(128, 0, 0);">script</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />  </span>
				<span style="color: rgb(0, 0, 255);">&lt;/</span>
				<span style="color: rgb(128, 0, 0);">head</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />  </span>
				<span style="color: rgb(0, 0, 255);">&lt;</span>
				<span style="color: rgb(128, 0, 0);">body </span>
				<span style="color: rgb(255, 0, 0);">onload</span>
				<span style="color: rgb(0, 0, 255);">=""</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />      </span>
				<span style="color: rgb(0, 0, 255);">&lt;</span>
				<span style="color: rgb(128, 0, 0);">INPUT </span>
				<span style="color: rgb(255, 0, 0);">id</span>
				<span style="color: rgb(0, 0, 255);">='xx' </span>
				<span style="color: rgb(255, 0, 0);">type</span>
				<span style="color: rgb(0, 0, 255);">="text"</span>
				<span style="color: rgb(255, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />      </span>
				<span style="color: rgb(0, 0, 255);">&lt;</span>
				<span style="color: rgb(128, 0, 0);">INPUT </span>
				<span style="color: rgb(255, 0, 0);">id</span>
				<span style="color: rgb(0, 0, 255);">='x' </span>
				<span style="color: rgb(255, 0, 0);">type</span>
				<span style="color: rgb(0, 0, 255);">="button"</span>
				<span style="color: rgb(255, 0, 0);"> onclick</span>
				<span style="color: rgb(0, 0, 255);">="getCustomerInfo()"</span>
				<span style="color: rgb(255, 0, 0);"> value</span>
				<span style="color: rgb(0, 0, 255);">="go"</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />  </span>
				<span style="color: rgb(0, 0, 255);">&lt;/</span>
				<span style="color: rgb(128, 0, 0);">body</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 255);">&lt;/</span>
				<span style="color: rgb(128, 0, 0);">html</span>
				<span style="color: rgb(0, 0, 255);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
				</span>
		</div>
		<br />为了方便明了我java方就使用了 server <br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);"> servlet;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.io.IOException;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.io.PrintWriter;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.servlet.ServletException;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.servlet.http.HttpServlet;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.servlet.http.HttpServletRequest;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> javax.servlet.http.HttpServletResponse;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.jxpath.JXPathContext;<br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> net.sf.json.JSONObject;<br /><br /><br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Json </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> HttpServlet {<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> doGet(HttpServletRequest req,HttpServletResponse rpo)</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> ServletException,IOException{<br />        rpo.setCharacterEncoding(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">GBK</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        req.setCharacterEncoding(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">GBK</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        rpo.setContentType(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">text/html; charset=GBK</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        <br />        PrintWriter out </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> rpo.getWriter() ;<br />        <br />         <font color="#006400">//得到 url 传入数据 </font><br />        String str </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> req.getParameter(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">jsonStr</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) ;<br />      <b>   <font color="#006400">//java 方 string -&gt; javaBean</font></b><br />        JSONObject jso </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> JSONObject.fromString(str);<br /><b><font color="#006400">        //javaBean 使用jxpathcontxt解读更方便 ^_^ <br />        //其实jso中是以 map 形式存区的 有兴趣的可以自己动手写下哦</font></b><br />        JXPathContext jx </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> JXPathContext.newContext(jso);<br />        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />                <font color="#006400">//jxpath好处来了 avg + 1</font><br />            jx.setValue(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">./avg</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, jx.getValue(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">./avg + 1 </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) );<br />        } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {e.printStackTrace();}<br />         <br />        <font color="#008000">//以 jsonString 传出</font><br />        out.print(jso.toString());<br /><br />    }<br />}<br /></span></div><br />web.xml（好象有点多嘴了 哈哈 方便下入门人了）<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">servlet</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">servlet-name</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">json</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">servlet-name</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">servlet-class</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">servlet.Json</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">servlet-class</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">servlet</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">servlet-mapping</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">servlet-name</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">json</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">servlet-name</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">url-pattern</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">/json</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">url-pattern</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">servlet-mapping</span><span style="color: rgb(0, 0, 255);">&gt;</span></div><br /><br /><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/144785.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-09-13 11:36 <a href="http://www.blogjava.net/Good-Game/archive/2007/09/13/144785.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>