JSON解析详细文档

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

JSON的含义?JSON的全称是JavaScriptObjectNotation,是一种轻量级的数据交换格式。JSON与XML具有相同的特性,例如易于人编写和阅读,易于机器生成和解析。但是JSON比XML数据传输的有效性要高出很多。JSON完全独立与编程语言,使用文本格式保存。JSON数据有两种结构:Name-Value对构成的集合,类似于Java中的Map。Value的有序列表,类似于Java中的Array。一个JSON格式的数据示例:{Name:Apple,Expiry:2007/10/1113:54,Price:3.99,Sizes:[Small,Medium,Large]}更多关于JSON数据格式的说明参看JSON官方网站:(中文内容参看:)GWT与JSONGWT中支持的客户端服务器端方法调用和数据传递的标准格式是RPC。JSON并不是GWT支持的标准的数据传递格式。那么如何使用JSON来作为GWT的数据传递格式呢?需要以下几步。第一,引用HTTP和JSON支持。第二,在客户端创建JSON数据,提交到服务器第三,在服务器上重写数据格式解析的代码,使之支持JSON格式的数据第四,在服务器上组织JSON格式的数据,返回给客户端。第五,客户端解析服务器传回的JSON数据,正确的显示引用HTTP和JSON支持找到.gwt.xml文件,在其中的inheritsname='com.google.gwt.user.User'/在之后添加如下的内容:inheritsname=com.google.gwt.json.JSON/inheritsname=com.google.gwt.http.HTTP/其中com.google.gwt.json.JSON指的是要使用JSON,com.google.gwt.http.HTTP值得是通过HTTP调用服务器上的服务方法。客户端构造JSON数据客户端需要使用com.google.gwt.json.client包内的类来组装JSON格式的数据,数据格式如下:数据类型说明JSONArrayJSONValue构成的数组类型JSONBooleanJSONboolean值JSONException访问JSON结构的数据出错的情况下可以抛出此异常JSONNullJSONNull根式的数据JSONNumberJSONNumber类型的数据JSONObjectJSONObject类型的数据JSONParser将String格式的JSON数据解析为JSONValue类型的数据JSONStringJSONString类型的数据JSONValue所有JSON类型值的超级类型组合一个简单的JSON数据:JSONObjectinput=newJSONObject();JSONStringvalue=newJSONString(mazhao);input.put(name,value);JSON数据格式为:{name:mazhao}组合一个包含数组类型的复杂JSON数据:JSONObjectinput=newJSONObject();JSONStringvalue=newJSONString(mazhao);input.put(name,value);JSONArrayarrayValue=newJSONArray();arrayValue.set(0,newJSONString(arrayitem0));arrayValue.set(1,newJSONString(arrayitem1));arrayValue.set(2,newJSONString(arrayitem2));input.put(array,arrayValue);JSON数据格式为:{name:mazhao,array:{arrayitem0,arrayitem1,arrayitem2}}注意上述的JSON类型的数据,使用的都是com.google.gwt.json.client包内的类型。这些类型最终会被编译为JavaScript执行。服务端重写数据解析代码,支持JSON格式的数据在服务器上,需要使用JSONJava支持类才能将JSON格式的数据转换为各种类型的数据,当然也可以自己写一些解析用的代码。这里我们使用了上的代码来完成。这组代码与com.google.gwt.json.client的代码很相似,只是在org.json包内部。怎么解析JSON术诀呢?针对上述中的复杂的JSON数据:{name:mazhao,array:{arrayitem0,arrayitem1,arrayitem2}}可以使用如下的方式解析:JSONObjectjsonObject=newJSONObject(payload);Stringname=jsonObject.getString(name);System.out.println(nameis:+name);JSONArrayjsonArray=jsonObject.getJSONArray(array);for(inti=0;ijsonArray.length();i++){System.out.println(item+i+:+jsonArray.getString(i));}其中payload指的是上述的JSON格式的数据。那么如何写GWT的Service来得到Payload的数据呢?需要两点,第一,需要建立一个Service类,第二,覆盖父类的processCall方法。示例代码:packagecom.jpleasure.gwt.json.server;importcom.google.gwt.user.client.rpc.SerializationException;importcom.google.gwt.user.server.rpc.RemoteServiceServlet;importcom.jpleasure.gwt.json.client.HelloWorldService;importorg.json.JSONArray;importorg.json.JSONException;importorg.json.JSONObject;/***CreatedbyIntelliJIDEA.*User:vaio*Date:2007-9-4*Time:22:08:31*TochangethistemplateuseFile|Settings|FileTemplates.*/publicclassHelloWorldServiceImplextendsRemoteServiceServletimplementsHelloWorldService{publicStringprocessCall(Stringpayload)throwsSerializationException{try{JSONObjectjsonObject=newJSONObject(payload);Stringname=jsonObject.getString(name);System.out.println(nameis:+name);JSONArrayjsonArray=jsonObject.getJSONArray(array);for(inti=0;ijsonArray.length();i++){System.out.println(item+i+:+jsonArray.getString(i));}}catch(JSONExceptione){e.printStackTrace();//TochangebodyofcatchstatementuseFile|Settings|FileTemplates.}returnsuccess;}}在服务器上组织JSON格式的数据,返回给客户端同上客户端解析服务器传回的JSON数据,正确的显示同上Struts2返回json需要jsonplugin-0[1].25的包然后我们的配置文件中需要继承json-defaultJava代码1.?xmlversion=1.0encoding=UTF-8?2.!DOCTYPEstrutsPUBLIC3.-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN4.=com.action.testJsonextends=json-defaultnamespace=/9.actionname=jsonUserclass=com.action.testJson.JsonActionmethod=testUser10.resulttype=json/11./action12.!--Addactionshere--13./package14./struts?xmlversion=1.0encoding=UTF-8?!DOCTYPEstrutsPUBLIC-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN=com.action.testJsonextends=json-defaultnamespace=/actionname=jsonUserclass=com.action.testJson.JsonActionmethod=testUserresulttype=json//action!--Addactionshere--/package/struts然后我们的Action中需要返回的json信息需要加上注解Java代码1.//pizza2.packagecom.action.testJson;3.4.importjava.util.ArrayList;5.importjava.util.List;6.7.importcom.googlecode.jsonplugin.annotations.JSON;8.importcom.opensymphony.xwork2.ActionSupport;9.10.publicclassJsonActionextendsActionSupport{11.12.privatestaticfinallongserialVersionUID=-4082165361641669835L;13.14.Usersuser=newUsers();15.ListuserList=newArrayList();16.17.18.publicStringtestUser(){19.System.out.println(inthejsonActon);20.userInit();21.userList.add(user);22.returnSUCCESS;23.}24.25.publicvoiduserInit(){26.user.setAge(1);27.user.setName(张泽峰);28.user.setPassword(nofengPassword);29.}30.31.@JSON(name=userString)32.publicUsersgetUser(){33.returnuser;34.}35.36.@JSON(name=userList)37.publicListgetUserList(){38.returnuserList;39.}40.41.publicvoidsetUser(Usersuser){4

1 / 21
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功