- 浏览: 772249 次
- 性别:
- 来自: 西安
文章分类
最新评论
-
wangyudong:
新版本 Wisdom RESTClienthttps://gi ...
jersery client调用REST框架web services服务的一个示例 -
wangyudong:
很多REST Client是不支持自动化测试RESTful A ...
jersery client调用REST框架web services服务的一个示例 -
doubledumbao:
感谢你的代码,在使用中发现两处小问题,已经做了修改,再次感谢。 ...
Java Zip Utils 压缩/解压缩工具包 -
doubledumbao:
package com.ry.messagedigest;
...
Java Zip Utils 压缩/解压缩工具包 -
phrmgb:
对wsdl讲解的很细致,收藏
Web Service (二) WSDL详解
因为gson网上的帮助文档打开时比较慢,所以把帮助文档摘录如此,方便查看:
1. 基本类型转化
public static void main(String[] args) { Gson gson = new Gson(); System.out.println(gson.toJson(1)); // ==> prints 1 System.out.println(gson.toJson("abcd"));// ==> prints "abcd" System.out.println(gson.toJson(new Long(10)));// ==> prints 10 int[] values = { 1 }; System.out.println(gson.toJson(values));// ==> prints [1] System.out.println("============"); int one = gson.fromJson("1", int.class); Integer one1 = gson.fromJson("1", Integer.class); Long one2 = gson.fromJson("1", Long.class); String str = gson.fromJson("\"abc\"", String.class); String anotherStr = gson.fromJson("[\"abc\"]", String.class); int[] ints = gson.fromJson("[1,2,3,4,5]", int[].class); Boolean b = gson.fromJson("false", Boolean.class); System.out.println(b == false); //==> prints true }
2.对象转化
public class BagOfPrimitives { private int value1 = 1; private String value2 = "abc"; //是用于声明变量在序列化的时候不被存储 private transient int value3 = 3; BagOfPrimitives() { // no-args constructor } public static void main(String[] args) { BagOfPrimitives obj = new BagOfPrimitives(); Gson gson = new Gson(); String json = gson.toJson(obj); System.out.println(json); //==> json is {"value1":1,"value2":"abc"} BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class); System.out.println(obj2.value1); System.out.println(obj2.value2); System.out.println(obj2.value3);//==>3 String json1 = "{'value1':1,'value2':'abc','value3':4}"; BagOfPrimitives obj3 = gson.fromJson(json1, BagOfPrimitives.class); System.out.println(obj3.value1); System.out.println(obj3.value2); System.out.println(obj3.value3); //==>3 } }
Note that you can not serialize objects with circular references since that will result in infinite recursion.
如果要是用json lib的话,上面的代码可以写成如下所示:
String json1 = "{'value1':1,'value2':'abc','value3':4}"; JSONObject jsonObj = JSONObject.fromObject( json1 ); BagOfPrimitives obj3 = (BagOfPrimitives) JSONObject.toBean( jsonObj, BagOfPrimitives.class );
Finer Points with Objects
- It is perfectly fine (and recommended) to use private fields
- There
is no need to use any annotations to indicate a field is to be included
for serialization and deserialization. All fields in the current class
(and from all super classes) are included by default.
- If a field is marked transient, (by default) it is ignored and not included in the JSON serialization or deserialization.
- This implementation handles nulls correctly
- While serialization, a null field is skipped from the output
- While deserialization, a missing entry in JSON results in setting the corresponding field in the object to null
- If a field is synthetic
, it is ignored and not included in JSON serialization or deserialization
- Fields corresponding to the outer classes in inner classes, anonymous classes, and local classes are ignored and not included in serialization or deserialization
Nested Classes (including Inner Classes)
public class A { public String a; class B { public String b; public B() { // No args constructor for B } } }
NOTE : The above class B can not (by default) be serialized with Gson.
G s o n c a n n o t d e serialize {"b":"abc"} into an instance of B since the class B is an inner class. if it was defined as static class B then Gson would have been able to deserialize the string. Another solution is to write a custom instance creator for B.
public class InstanceCreatorForB implements InstanceCreator<A.B> { private final A a; public InstanceCreatorForB(A a) { this.a = a; } public A.B createInstance(Type type) { return a.new B(); } }
The above is possible, but not recommended.
数组例子:
Gson gson = new Gson(); int[] ints = {1, 2, 3, 4, 5}; String[] strings = {"abc", "def", "ghi"}; (Serialization) gson.toJson(ints); ==> prints [1,2,3,4,5] gson.toJson(strings); ==> prints ["abc", "def", "ghi"] (Deserialization) int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class); ==> ints2 will be same as ints
We also support multi-dimensional arrays, with arbitrarily complex element types
综合实例1:
public class ExampleBean { private String name; private String id; private int age; private boolean isOk; public ExampleBean(String name, String id, int age, boolean isOk) { super(); this.name = name; this.id = id; this.age = age; this.isOk = isOk; } public ExampleBean() { } //setter和getter方法 }
测试:
public static void main(String[] args) { Gson gson = new Gson(); List<ExampleBean> list = new ArrayList<ExampleBean>(); for (int i = 0; i < 5; i++) { String name = "xxlong" + i; int age = 20 + i; ExampleBean bean = new ExampleBean(name, i + "", age); list.add(bean); } Type listType = new TypeToken<List<ExampleBean>>() { }.getType(); //将list转化成json字符串 String json = gson.toJson(list); System.out.println(json); //将json字符串还原成list List<ExampleBean> list2 = gson.fromJson(json, listType); }
输出如下:[{"name":"xxlong0","id":"0","age":20,"isOk":false},{"name":"xxlong1","id":"1","age":21,"isOk":false},{"name":"xxlong2","id":"2","age":22,"isOk":false},{"name":"xxlong3","id":"3","age":23,"isOk":false},{"name":"xxlong4","id":"4","age":24,"isOk":false}]
综合实例2:
需求:想将字符串{'tableName' :'ys_index_y','year': '2008','params':'[z_expense,z_expense_profit,z_main_margin]','isOperAll':'false','copyToYear':''}还原成对象OpeerConditions,OpeerConditions对象代码如下所示:
public class OperConditions { private String tableName; private String year; private String[] params; private boolean isOperALl; private String copyToYear; public OperConditions() { } public OperConditions(String tableName, String year, String[] params, boolean isOperALl, String copyToYear) { super(); this.tableName = tableName; this.year = year; this.params = params; this.setOperALl(isOperALl); this.copyToYear = copyToYear; } //getter和setter方法 }
因为OperConditions中有属性params,它是一个数组,所以无论是用json lib还是gson都不能直接将上面的字符串还原成OperCondtions对象,可以直接将params分离出来,单独处理,我这里选用此种方法来处理:
json-lib代码如下:
public static void main(String[] args) { String json = "{'tableName' :'ys_index_y','year': '2008','isOperAll':'false','copyToYear':''}"; JSONObject jsonObj = JSONObject.fromObject( json ); OperConditions conditions = (OperConditions) JSONObject.toBean( jsonObj, OperConditions.class ); System.out.println(conditions.isOperALl() == false); //==>输出为true String json1 = "['z_expense','z_expense_profit','z_main_margin']"; JSONArray jsonArray = JSONArray.fromObject(json1); //List<String> list = jsonArray.toList(jsonArray); //这个方法也可以 List<String> list = jsonArray.toList(jsonArray,String.class); conditions.setParams(list.toArray(new String[0])); System.out.println(conditions.getParams()[0]); //==>输出为z_expense }
因为JSONArray的toArray()方法返回的是一个Object[]数组,所以先将它转化成list,再转化到String数组。
当然由JSONArray转化成list时也可以使用subList方法,如下所示:
List<String> list = jsonArray.subList(0, jsonArray.size());
或者可以直接使用JSONArray的iterator()
方法迭代它本身直接得到需要的String数组。
如果使用Gson来完成这一需求,个人感觉更简单,代码如下所示:
public static void main(String[] args) { String json = "{'tableName' :'ys_index_y','year': '2008','isOperAll':'false','copyToYear':''}"; Gson gson = new Gson(); OperConditions conditions = gson.fromJson(json, OperConditions.class); System.out.println(conditions.isOperALl() == false); // ==>输出为true String json1 = "['z_expense','z_expense_profit','z_main_margin']"; String[] params = gson.fromJson(json1,String[].class); conditions.setParams(params); System.out.println(conditions.getParams()[0]); // ==>输出为z_expense }
Gson可以直接转化成String[]数组,同时转化OperConditions时也比json-lib简单。
还有一点是非常值得注意的,就是你的bean中有boolean属性值时,强烈建议你别像我这个例子中一样命名为以is开头的属性名,这可能给你带来意想不到的错误,关于这一点的详细解说请参看我的文章json lib 学习笔记
发表评论
-
JS回到顶部效果
2013-05-22 23:01 1198<%@ page contentType=" ... -
JS将网站添加到收藏夹
2013-05-22 21:23 3761function addToFavorite(){ ... -
企信通项目前端开发笔记
2012-10-09 15:38 0/* * 根据元素标签名及类名查找符合要求的 ... -
选择框(select)添加或者减少选项(option)操作的js代码
2012-10-09 15:40 2022选择框(select)添加或者减少选项(option)操作的j ... -
常用的js较验集锦
2011-12-12 10:08 10001.去空格 第一种方法 function ltrim ... -
最近做项目遇到的有关jqGrid和jstree的问题
2010-05-24 17:44 4152一、query.jqGrid-3.6.4 css: 首 ... -
JavaScript concat方法和join方法及性能测试
2010-03-24 14:07 2673一. concat方法 1. String中的conca ... -
jquery性能优化技巧(二)
2010-03-23 18:02 1833本文转载自: http://www.b ... -
jquery性能优化技巧(一)
2010-03-23 17:37 2428本文参考自: http://www.b ... -
强烈推荐:240多个jQuery插件
2010-03-23 13:32 950本文转载自:http://www.cnblogs.com/te ... -
json lib 学习笔记
2010-01-30 12:01 8385json-lib demo JSON-lib这个Ja ... -
JQuery UI accordion学习笔记
2010-01-09 15:44 9293JQuery UI accordion学习笔记 ...
相关推荐
Java常用四大json库,包含目前最新版gson,fastjson,jackson,json-lib
jar包如下:commons-beanutils-1.7,commons-collections,commons-lang,commons-logging-1.1.1,ezmorph, gson-2.2.1,gson-2.2.1-sources,json-lib-2.2.2-jdk15
本项目是gson,fastJson,json-lib实例,包含完整代码和jar包,亲测可用,已经用于公司项目开发中。
包含gson解析json,JSONArray,JSONObject解析用到的gson-2.3.1.jar,json-lib-2.3-jdk15.jar,commons-lang-2.0.jar,commons-logging-1.1.1.jar,ezmorph-1.0.2.jar,commons-collections-3.2.jar,commons-...
json.jar,gson.jar,Junit.jar,json-lib及其依赖包下载,亲测可用
介绍:Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. 压缩包里有以下文件: ...
本项目是完整项目,内含完整代码和jar包,亲测可用,已用于公司项目中。
google-json-gson-master,google的jsonjar包,提供json方法。
赠送jar包:gson-2.8.7.jar; 赠送原API文档:gson-2.8.7-javadoc.jar; 赠送源代码:gson-2.8.7-sources.jar; 赠送Maven依赖信息文件:gson-2.8.7.pom; 包含翻译后的API文档:gson-2.8.7-javadoc-API文档-中文...
赠送jar包:gson-2.8.0.jar; 赠送原API文档:gson-2.8.0-javadoc.jar; 赠送源代码:gson-2.8.0-sources.jar; 赠送Maven依赖信息文件:gson-2.8.0.pom; 包含翻译后的API文档:gson-2.8.0-javadoc-API文档-中文...
google-gson-2.2.4.zip
赠送jar包:gson-2.8.5.jar; 赠送原API文档:gson-2.8.5-javadoc.jar; 赠送源代码:gson-2.8.5-sources.jar; 赠送Maven依赖信息文件:gson-2.8.5.pom; 包含翻译后的API文档:gson-2.8.5-javadoc-API文档-中文...
NULL 博文链接:https://jilongliang.iteye.com/blog/1909209
赠送jar包:gson-2.8.6.jar; 赠送原API文档:gson-2.8.6-javadoc.jar; 赠送源代码:gson-2.8.6-sources.jar; 赠送Maven依赖信息文件:gson-2.8.6.pom; 包含翻译后的API文档:gson-2.8.6-javadoc-API文档-中文...
Gson-2.8.1的官方jar包合集【gson-2.8.1.jar,gson-2.8.1-sources.jar】, 包含Gson核心包和源码包。
赠送jar包:gson-2.8.9.jar; 赠送原API文档:gson-2.8.9-javadoc.jar; 赠送源代码:gson-2.8.9-sources.jar; 赠送Maven依赖信息文件:gson-2.8.9.pom; 包含翻译后的API文档:gson-2.8.9-javadoc-API文档-中文...
1.GSON的两个重要方法 1.将要解析的数据如下面的格式 2.必须定义如下一些的实体类数据
Gson-2.8.0的官方jar包合集【gson-2.8.0.jar,gson-2.8.0-sources.jar,gson-2.8.0-javadoc.jar】,JSON解析类库Gson库的jar包。
赠送jar包:gson-2.6.2.jar; 赠送原API文档:gson-2.6.2-javadoc.jar; 赠送源代码:gson-2.6.2-sources.jar; 包含翻译后的API文档:gson-2.6.2-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId:...