<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Power by Result Search &#187; json</title>
	<atom:link href="http://www.result-search.com/sty/category/technology/json/feed" rel="self" type="application/rss+xml" />
	<link>http://www.result-search.com/sty</link>
	<description>Just another weblog</description>
	<lastBuildDate>Wed, 23 Jun 2010 03:12:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>GWT与JSON(使用JSON格式的数据通讯)</title>
		<link>http://www.result-search.com/sty/2009/04/23/gwt-and-jsonusing-json-to-transfer-data.html</link>
		<comments>http://www.result-search.com/sty/2009/04/23/gwt-and-jsonusing-json-to-transfer-data.html#comments</comments>
		<pubDate>Thu, 23 Apr 2009 05:38:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.yaaahaaa.com/2009/04/23/gwt%e4%b8%8ejson%e4%bd%bf%e7%94%a8json%e6%a0%bc%e5%bc%8f%e7%9a%84%e6%95%b0%e6%8d%ae%e9%80%9a%e8%ae%af/</guid>
		<description><![CDATA[JSON 是什么？ 
JSON 的全称是JavaScript Object Notation，是一种轻量级的数据交换格式。JSON与XML具有相同的特性，例如易于人编写和阅读，易于机器生成和解析。但是JSON比XML数据 传输的有效性要高出很多。JSON完全独立与编程语言，使用文本格式保存。
JSON数据有两种结构：


Name-Value 对构成的集合，类似于Java中的Map。


Value的有序列表，类似于Java中的Array。


一个JSON格式的数据示例：
{
&#8220;Name&#8221;: &#8220;Apple&#8221;,
&#8220;Expiry&#8221;: &#8220;2007/10/11 13:54&#8243;,
&#8220;Price&#8221;: 3.99,
&#8220;Sizes&#8221;: [
"Small",
"Medium",
"Large"
]
}
更多关于JSON数据格式的说明参看JSON官方网站：http://www.json.org（中文内容参看：http://www.json.org/json-zh.html）
GWT与JSON
GWT中支持的客户端服务器端方法调用和数据传递的标准格式是RPC。 JSON并不是GWT支持的标准的数据传递格式。那么如何使用JSON来作为GWT的数据传递格式呢？需要以下几步。
第一，引用HTTP和JSON支持。
第二，在客户端创建JSON数据，提交到服务器
第三，在服务器上重写数据格式解析的代码，使之支持JSON格式的数据
第四，在服务器上组织JSON格式的数据，返回给客户端。
第五，客户端解析服务器传回的JSON数据，正确的显示
引用HTTP和JSON支持
找到.gwt.xml文件，在其中的
&#60;inherits name=&#8217;com.google.gwt.user.User&#8217;/&#62;
在之后添加如下的内容：
&#60;inherits name=&#8221;com.google.gwt.json.JSON&#8221;/&#62;
&#60;inherits name=&#8221;com.google.gwt.http.HTTP&#8221;/&#62;
其中com.google.gwt.json.JSON指的是要使用JSON，com.google.gwt.http.HTTP值得是通过HTTP调用服务器上的服务方法。
客户端构造JSON数据
客户端需要使用com.google.gwt.json.client包内的类来组装JSON格式的数据，数据格式如下：




数据类型
说明


JSONArray
JSONValue构成的数组类型


JSONBoolean
JSON boolean值


JSONException
访问JSON结构的数据出错的情况下可以抛出此异常


JSONNull
JSON Null根式的数据


JSONNumber
JSON Number类型的数据


JSONObject
JSON Object类型的数据


JSONParser
将String格式的JSON数据解析为JSONValue类型的数据


JSONString
JSON String类型的数据


JSONValue
所有JSON类型值的超级类型




组合一个简单的JSON数据：
JSONObject input = new JSONObject();
JSONString value = new JSONString(&#8220;mazhao&#8221;);
input.put(&#8220;name&#8221;, value);
JSON数据格式为：{name: &#8220;mazhao&#8221;}
组合一个包含数组类型的复杂JSON数据：
JSONObject input = new JSONObject();
JSONString value = new JSONString(&#8220;mazhao&#8221;);
input.put(&#8220;name&#8221;, value);
JSONArray arrayValue = new JSONArray();
arrayValue.set(0, new JSONString(&#8220;array item 0&#8243;));
arrayValue.set(1, new JSONString(&#8220;array item 1&#8243;));
arrayValue.set(2, new JSONString(&#8220;array [...]]]></description>
			<content:encoded><![CDATA[<p><strong>JSON 是什么？ </strong></p>
<p>JSON 的全称是JavaScript Object Notation，是一种轻量级的数据交换格式。JSON与XML具有相同的特性，例如易于人编写和阅读，易于机器生成和解析。但是JSON比XML数据 传输的有效性要高出很多。JSON完全独立与编程语言，使用文本格式保存。</p>
<p>JSON数据有两种结构：</p>
<ul>
<li>
<div>Name-Value 对构成的集合，类似于Java中的Map。</div>
</li>
<li>
<div>Value的有序列表，类似于Java中的Array。</div>
</li>
</ul>
<p>一个JSON格式的数据示例：</p>
<p>{<br />
&#8220;Name&#8221;: &#8220;Apple&#8221;,<br />
&#8220;Expiry&#8221;: &#8220;2007/10/11 13:54&#8243;,<br />
&#8220;Price&#8221;: 3.99,<br />
&#8220;Sizes&#8221;: [<br />
"Small",<br />
"Medium",<br />
"Large"<br />
]<br />
}</p>
<p>更多关于JSON数据格式的说明参看JSON官方网站：<a href="http://www.json.org/">http://www.json.org</a>（中文内容参看：<a href="http://www.json.org/json-zh.html"><span style="color: #551a8b;">http://www.json.org/json-zh.html</span></a>）</p>
<p><strong>GWT与JSON</strong></p>
<p>GWT中支持的客户端服务器端方法调用和数据传递的标准格式是RPC。 JSON并不是GWT支持的标准的数据传递格式。那么如何使用JSON来作为GWT的数据传递格式呢？需要以下几步。</p>
<p>第一，引用HTTP和JSON支持。</p>
<p>第二，在客户端创建JSON数据，提交到服务器</p>
<p>第三，在服务器上重写数据格式解析的代码，使之支持JSON格式的数据</p>
<p>第四，在服务器上组织JSON格式的数据，返回给客户端。</p>
<p>第五，客户端解析服务器传回的JSON数据，正确的显示</p>
<p><strong>引用HTTP和JSON支持</strong></p>
<p>找到.gwt.xml文件，在其中的</p>
<p>&lt;inherits name=&#8217;com.google.gwt.user.User&#8217;/&gt;</p>
<p>在之后添加如下的内容：</p>
<p>&lt;inherits name=&#8221;com.google.gwt.json.JSON&#8221;/&gt;<br />
&lt;inherits name=&#8221;com.google.gwt.http.HTTP&#8221;/&gt;</p>
<p>其中com.google.gwt.json.JSON指的是要使用JSON，com.google.gwt.http.HTTP值得是通过HTTP调用服务器上的服务方法。</p>
<p><strong>客户端构造JSON数据</strong></p>
<p>客户端需要使用com.google.gwt.json.client包内的类来组装JSON格式的数据，数据格式如下：</p>
<div>
<table class="zeroBorder" border="0" cellspacing="0" cellpadding="3" width="100%">
<tbody>
<tr>
<td width="50%"><strong>数据类型</strong></td>
<td width="50%"><strong>说明</strong></td>
</tr>
<tr>
<td width="50%">JSONArray</td>
<td width="50%">JSONValue构成的数组类型</td>
</tr>
<tr>
<td width="50%">JSONBoolean</td>
<td width="50%">JSON boolean值</td>
</tr>
<tr>
<td width="50%">JSONException</td>
<td width="50%">访问JSON结构的数据出错的情况下可以抛出此异常</td>
</tr>
<tr>
<td width="50%">JSONNull</td>
<td width="50%">JSON Null根式的数据</td>
</tr>
<tr>
<td width="50%">JSONNumber</td>
<td width="50%">JSON Number类型的数据</td>
</tr>
<tr>
<td width="50%">JSONObject</td>
<td width="50%">JSON Object类型的数据</td>
</tr>
<tr>
<td width="50%">JSONParser</td>
<td width="50%">将String格式的JSON数据解析为JSONValue类型的数据</td>
</tr>
<tr>
<td width="50%">JSONString</td>
<td width="50%">JSON String类型的数据</td>
</tr>
<tr>
<td width="50%">JSONValue</td>
<td width="50%">所有JSON类型值的超级类型</td>
</tr>
</tbody>
</table>
</div>
<p>组合一个简单的JSON数据：</p>
<p>JSONObject input = new JSONObject();<br />
JSONString value = new JSONString(&#8220;mazhao&#8221;);<br />
input.put(&#8220;name&#8221;, value);</p>
<p>JSON数据格式为：{name: &#8220;mazhao&#8221;}</p>
<p>组合一个包含数组类型的复杂JSON数据：</p>
<p>JSONObject input = new JSONObject();<br />
JSONString value = new JSONString(&#8220;mazhao&#8221;);<br />
input.put(&#8220;name&#8221;, value);</p>
<p>JSONArray arrayValue = new JSONArray();<br />
arrayValue.set(0, new JSONString(&#8220;array item 0&#8243;));<br />
arrayValue.set(1, new JSONString(&#8220;array item 1&#8243;));<br />
arrayValue.set(2, new JSONString(&#8220;array item 2&#8243;));<br />
input.put(&#8220;array&#8221;, arrayValue);</p>
<p>JSON数据格式为：</p>
<p>{name: &#8220;mazhao&#8221;,</p>
<p>array: {&#8220;array item 0&#8243;, &#8220;array item 1&#8243;, &#8220;array item 2&#8243;}}</p>
<p>注意上述的JSON类型的数据，使用的都是com.google.gwt.json.client包内的类型。这些类型最终会被编译为JavaScript执行。</p>
<p><strong>服务端重写数据解析代码，支持JSON格式的数据</strong></p>
<p>在服务器上，需要使用JSON Java支持类才能将JSON格式的数据转换为各种类型的数据，当然也可以自己写一些解析用的代码。这里我们使用了<a href="http://www.json.org/">www.json.org</a>上的代码来完成。这组代码与com.google.gwt.json.client的代码很相似，只是在org.json包内部。</p>
<p>怎么解析JSON术诀呢？针对上述中的复杂的JSON数据：</p>
<p>{name: &#8220;mazhao&#8221;,</p>
<p>array: {&#8220;array item 0&#8243;, &#8220;array item 1&#8243;, &#8220;array item 2&#8243;}}</p>
<p>可以使用如下的方式解析：</p>
<p>JSONObject jsonObject = new JSONObject(payload);<br />
String name = jsonObject.getString(&#8220;name&#8221;);</p>
<p>System.out.println(&#8220;name is:&#8221; + name);<br />
JSONArray jsonArray = jsonObject.getJSONArray(&#8220;array&#8221;);<br />
for(int i = 0; i &lt; jsonArray.length(); i++) {<br />
System.out.println(&#8220;item &#8221; + i + &#8221; :&#8221; + jsonArray.getString(i));<br />
}</p>
<p>其中payload指的是上述的JSON格式的数据。</p>
<p>那么如何写GWT 的Service来得到Payload的数据呢？需要两点，第一，需要建立一个Service类，第二，覆盖父类的processCall方法。</p>
<p>示例代码：</p>
<p>package com.jpleasure.gwt.json.server;</p>
<p>import com.google.gwt.user.client.rpc.SerializationException;<br />
import com.google.gwt.user.server.rpc.RemoteServiceServlet;<br />
import com.jpleasure.gwt.json.client.HelloWorldService;<br />
import org.json.JSONArray;<br />
import org.json.JSONException;<br />
import org.json.JSONObject;</p>
<p>/**<br />
* Created by IntelliJ IDEA.<br />
* User: vaio<br />
* Date: 2007-9-4<br />
* Time: 22:08:31<br />
* To change this template use File | Settings | File Templates.<br />
*/<br />
public class HelloWorldServiceImpl extends RemoteServiceServlet implements HelloWorldService {<br />
public String processCall(String payload) throws SerializationException {<br />
try {<br />
JSONObject jsonObject = new JSONObject(payload);<br />
String name = jsonObject.getString(&#8220;name&#8221;);</p>
<p>System.out.println(&#8220;name is:&#8221; + name);<br />
JSONArray jsonArray = jsonObject.getJSONArray(&#8220;array&#8221;);<br />
for(int i = 0; i &lt; jsonArray.length(); i++) {<br />
System.out.println(&#8220;item &#8221; + i + &#8221; :&#8221; + jsonArray.getString(i));<br />
}<br />
} catch (JSONException e) {<br />
e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.<br />
}</p>
<p>return &#8220;success&#8221;;<br />
}<br />
}</p>
<p><strong>在服务器上组织JSON格式的数据，返回给客户端</strong></p>
<p>同上</p>
<p><strong>客户端解析服务器传回的JSON数据，正确的显示</strong></p>
<p>同上</p>
]]></content:encoded>
			<wfw:commentRss>http://www.result-search.com/sty/2009/04/23/gwt-and-jsonusing-json-to-transfer-data.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
