ImageVerifierCode 换一换
格式:DOCX , 页数:23 ,大小:35.16KB ,
资源ID:7455329      下载积分:12 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7455329.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(JSON解析详细文档.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

JSON解析详细文档.docx

1、JSON解析详细文档JSON 的含义?JSON的全称是JavaScript Object Notation,是一种轻量级的数据交换格式。JSON与XML具有相同的特性,例如易于人编写和阅读,易于机器生成和解析。但是JSON比XML数据传输的有效性要高出很多。JSON完全独立与编程语言,使用文本格式保存。JSON数据有两种结构: Name-Value 对构成的集合,类似于Java中的Map。 Value的有序列表,类似于Java中的Array。一个JSON格式的数据示例: Name: Apple, Expiry: 2007/10/11 13:54, Price: 3.99, Sizes: Sma

2、ll, Medium, Large 更多关于JSON数据格式的说明参看JSON官方网站:http:/www.json.org(中文内容参看:http:/www.json.org/json-zh.html)GWT与JSONGWT中支持的客户端服务器端方法调用和数据传递的标准格式是RPC。JSON并不是GWT支持的标准的数据传递格式。那么如何使用JSON来作为GWT的数据传递格式呢?需要以下几步。第一,引用HTTP和JSON支持。第二,在客户端创建JSON数据,提交到服务器第三,在服务器上重写数据格式解析的代码,使之支持JSON格式的数据第四,在服务器上组织JSON格式的数据,返回给客户端。第五,

3、客户端解析服务器传回的JSON数据,正确的显示引用HTTP和JSON支持找到.gwt.xml文件,在其中的在之后添加如下的内容: 其中com.google.gwt.json.JSON指的是要使用JSON,com.google.gwt.http.HTTP值得是通过HTTP调用服务器上的服务方法。客户端构造JSON数据客户端需要使用com.google.gwt.json.client包内的类来组装JSON格式的数据,数据格式如下:数据类型说明JSONArrayJSONValue构成的数组类型JSONBoolean JSON boolean值JSONException 访问JSON结构的数据出错的情

4、况下可以抛出此异常JSONNull JSON Null根式的数据JSONNumber JSON Number类型的数据JSONObject JSON Object类型的数据JSONParser 将String格式的JSON数据解析为JSONValue类型的数据JSONStringJSON String类型的数据JSONValue所有JSON类型值的超级类型组合一个简单的JSON数据: JSONObject input = new JSONObject();JSONString value = new JSONString(mazhao);input.put(name, value);JSON数

5、据格式为:name: mazhao组合一个包含数组类型的复杂JSON数据:JSONObject input = new JSONObject();JSONString value = new JSONString(mazhao);input.put(name, value);JSONArray arrayValue = new JSONArray();arrayValue.set(0, new JSONString(array item 0);arrayValue.set(1, new JSONString(array item 1);arrayValue.set(2, new JSONStr

6、ing(array item 2);input.put(array, arrayValue); JSON数据格式为:name: mazhao, array: array item 0, array item 1, array item 2注意上述的JSON类型的数据,使用的都是com.google.gwt.json.client包内的类型。这些类型最终会被编译为JavaScript执行。服务端重写数据解析代码,支持JSON格式的数据在服务器上,需要使用JSON Java支持类才能将JSON格式的数据转换为各种类型的数据,当然也可以自己写一些解析用的代码。这里我们使用了www.json.org上

7、的代码来完成。这组代码与com.google.gwt.json.client的代码很相似,只是在org.json包内部。怎么解析JSON术诀呢?针对上述中的复杂的JSON数据:name: mazhao, array: array item 0, array item 1, array item 2可以使用如下的方式解析:JSONObject jsonObject = new JSONObject(payload);String name = jsonObject.getString(name);System.out.println(name is: + name);JSONArray json

8、Array = jsonObject.getJSONArray(array);for(int i = 0; i jsonArray.length(); i+) System.out.println(item + i + : + jsonArray.getString(i);其中payload指的是上述的JSON格式的数据。那么如何写GWT 的Service来得到Payload的数据呢?需要两点,第一,需要建立一个Service类,第二,覆盖父类的processCall方法。示例代码:package com.jpleasure.gwt.json.server;import com.google.

9、gwt.user.client.rpc.SerializationException;import com.google.gwt.user.server.rpc.RemoteServiceServlet;import com.jpleasure.gwt.json.client.HelloWorldService;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;/* Created by IntelliJ IDEA.* User: vaio* Date: 2007-9-4* Ti

10、me: 22:08:31* To change this template use File | Settings | File Templates.*/public class HelloWorldServiceImpl extends RemoteServiceServlet implements HelloWorldService public String processCall(String payload) throws SerializationException try JSONObject jsonObject = new JSONObject(payload); Strin

11、g name = jsonObject.getString(name); System.out.println(name is: + name); JSONArray jsonArray = jsonObject.getJSONArray(array); for(int i = 0; i jsonArray.length(); i+) System.out.println(item + i + : + jsonArray.getString(i); catch (JSONException e) e.printStackTrace(); /To change body of catch sta

12、tement use File | Settings | File Templates. return success; 在服务器上组织JSON格式的数据,返回给客户端同上客户端解析服务器传回的JSON数据,正确的显示同上Struts2返回json需要jsonplugin-01.25的包 然后我们的配置文件中需要继承json-defaultJava代码 1. 2. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 然后我们的Action中需要返回的json信息需要加上注解 Java代码 1. /pizza 2. packagecom.action.testJson; 3.

13、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=newU

14、sers(); 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.

15、 JSON(name=userString) 32. publicUsersgetUser() 33. returnuser; 34. 35. 36. JSON(name=userList) 37. publicListgetUserList() 38. returnuserList; 39. 40. 41. publicvoidsetUser(Usersuser) 42. this.user=user; 43. 44. 45. publicvoidsetUserList(ListuserList) 46. this.userList=userList; 47. 48. 49. 50. JSO

16、N Plugin的说明Edit Page Browse Space Add Page Add News Added by Musachy Barroso, last edited by ghostroller on Jul 04, 2008 (view change) show comment hide comment Comment: change ignoreInterfaces to ignoreSMDMethodInterfaces in interceptor parametersView page history Name JSON Plugin Publisher Musachy

17、 Barroso License Open Source (ASL2) Version 0.30 CompatibilityStruts 2.0.6 or laterHomepage Download rate-34268-90672Rating? 1 2 3 4 5 OverviewThe JSON plugin provides a json result type that serializes actions into JSON. The serialization process is recursive, meaning that the whole object graph, s

18、tarting on the action class (base class not included) will be serialized (root object can be customized using the root attribute). If the interceptor is used, the action will be populated from the JSON content in the request, these are the rules of the interceptor:1. The content-type must be applica

19、tion/json 2. The JSON content must be well formed, see json.org for grammar. 3. Action must have a public setter method for fields that must be populated. 4. Supported types for population are: Primitives (int,long.String), Date, List, Map, Primitive Arrays, Other class (more on this later), and Arr

20、ay of Other class. 5. Any object in JSON, that is to be populated inside a list, or a map, will be of type Map (mapping from properties to values), any whole number will be of type Long, any decimal number will be of type Double, and any array of type List. Given this JSON string: doubleValue: 10.10

21、, nestedBean: name: Mr Bean , list: A, 10, 20.20, firstName: El Zorro , array: 10, 20 The action must have a setDoubleValue method, taking either a float or a double argument (the interceptor will convert the value to the right one). There must be a setNestedBean whose argument type can be any class

22、, that has a setName method taking as argument an String. There must be a setList method that takes a List as argument, that list will contain: A (String), 10 (Long), 20.20 (Double), Map (firstName - El Zorro). The setArray method can take as parameter either a List, or any numeric array.Installatio

23、nThis plugin can be installed by copying the plugin jar into your applications /WEB-INF/lib directory. No other files need to be copied or created.To use maven, add this to your pom: . com.googlecode jsonplugin 0.26 . Maven Plugin Repository http:/struts2plugin-maven- false true Customizing Serialization and DeserializationUse the JSON annotation to customize the serialization/deserialization process. Available JSON annotation fields:NameDescriptionDefault ValueSerializationDeserializationnameCustomize field nameemptyyesnoserializeInclude in serializationtrueyesnodeseri

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1