JSON解析详细文档

上传人:ji****72 文档编号:37521418 上传时间:2018-04-17 格式:DOC 页数:21 大小:214.50KB
返回 下载 相关 举报
JSON解析详细文档_第1页
第1页 / 共21页
JSON解析详细文档_第2页
第2页 / 共21页
JSON解析详细文档_第3页
第3页 / 共21页
JSON解析详细文档_第4页
第4页 / 共21页
JSON解析详细文档_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《JSON解析详细文档》由会员分享,可在线阅读,更多相关《JSON解析详细文档(21页珍藏版)》请在金锄头文库上搜索。

1、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“

2、: 3.99,“Sizes“: “Small“,“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 数据,提交到服务器第三,在服务器上重写数据格式解析的代

3、码,使之支持 JSON 格式的数据第四,在服务器上组织 JSON 格式的数据,返回给客户端。第五,客户端解析服务器传回的 JSON 数据,正确的显示引用引用 HTTP 和和 JSON 支持支持找到.gwt.xml 文件,在其中的在之后添加如下的内容:其中 com.google.gwt.json.JSON 指的是要使用 JSON,com.google.gwt.http.HTTP 值得是通过 HTTP 调用服务器上的服务方法。客户端构造客户端构造 JSON 数据数据客户端需要使用 com.google.gwt.json.client 包内的类来组装 JSON 格式的数据,数据格式如下:数据类型数据

4、类型说明说明JSONArrayJSONValue 构成的数组类型JSONBoolean JSON boolean 值JSONException 访问 JSON 结构的数据出错的情况下可以抛出此异常JSONNull JSON Null 根式的数据JSONNumber JSON Number 类型的数据JSONObject JSON Object 类型的数据JSONParser 将 String 格式的 JSON 数据解析为 JSONValue 类型的数据JSONStringJSON String 类型的数据JSONValue所有 JSON 类型值的超级类型组合一个简单的 JSON 数据: JSO

5、NObject input = new JSONObject();JSONString value = new JSONString(“mazhao“);input.put(“name“, value);JSON 数据格式为:name: “mazhao“组合一个包含数组类型的复杂 JSON 数据:JSONObject input = new JSONObject();JSONString value = new JSONString(“mazhao“);input.put(“name“, value);JSONArray arrayValue = new JSONArray();arrayVa

6、lue.set(0, new JSONString(“array item 0“);arrayValue.set(1, new JSONString(“array item 1“);arrayValue.set(2, new JSONString(“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.clien

7、t 包内的类型。这些类型最终会被编译为 JavaScript 执行。服务端重写数据解析代码,支持服务端重写数据解析代码,支持 JSON 格式的数据格式的数据在服务器上,需要使用 JSON Java 支持类才能将 JSON 格式的数据转换为各种类型的数据,当然也可以自己写一些解析用的代码。这里我们使用了 www.json.org 上的代码来完成。这组代码与 com.google.gwt.json.client 的代码很相似,只是在 org.json 包内部。怎么解析 JSON 术诀呢?针对上述中的复杂的 JSON 数据:name: “mazhao“,array: “array item 0“,

8、“array item 1“, “array item 2“可以使用如下的方式解析:JSONObject jsonObject = new JSONObject(payload);String name = jsonObject.getString(“name“);System.out.println(“name is:“ + name);JSONArray jsonArray = jsonObject.getJSONArray(“array“);for(int i = 0; i 2. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 然后我们的 Action 中需要返回的

9、 json 信息需要加上注解 Java 代码 1. /pizza 2. package com.action.testJson; 3. 4. import java.util.ArrayList; 5. import java.util.List; 6. 7. import com.googlecode.jsonplugin.annotations.JSON; 8. import com.opensymphony.xwork2.ActionSupport; 9. 10.public class JsonAction extends ActionSupport 11. 12. private s

10、tatic final long serialVersionUID = - 4082165361641669835L; 13. 14. Users user=new Users(); 15. List userList=new ArrayList(); 16. 17. 18. public String testUser() 19. System.out.println(“in the json Acton“); 20. userInit(); 21. userList.add(user); 22. return SUCCESS; 23. 24. 25. public void userIni

11、t() 26. user.setAge(1); 27. user.setName(“张泽峰“); 28. user.setPassword(“nofengPassword“); 29. 30. 31. JSON(name=“userString“) 32. public Users getUser() 33. return user; 34. 35. 36. JSON(name=“userList“) 37. public List getUserList() 38. return userList; 39. 40. 41. public void setUser(Users user) 42

12、. this.user = user; 43. 44. 45. public void setUserList(List userList) 46. this.userList = userList; 47. 48. 49. 50. JSON 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 Name JSON Plugin Publisher Mu

13、sachy Barroso License Open Source (ASL2) Version 0.30 CompatibilityStruts 2.0.6 or laterHomepage http:/ Download http:/ Rating?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 grap

14、h, starting 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 “application/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 cl

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号