转http://www.rsky.com.cn/Article/java/201005/11770.html
在B/S网络编程开发中,后台利用Java解析或者生成JSON,与前端页面的交互的任务可以利用一些开源的小jar包解决,当然自己写一些简单的也是可行的,最近发现了GSON这个Google开发的,不是必须使用annotation,很好用,下面简单记录下用法。
Gson is a Java library that can be used to convert Java Objects into its JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
There are a few open-source projects that can convert Java objects to JSON. However, most of them require that you place Java annotations in your classes something that you can not do if you do not have access to the source-code. Most also do not fully support the use of Java Generics. Gson considers both of these as very important design goals.
GSON: http://code.google.com/p/google-gson/
GSON API: http://google-gson.googlecode.com/svn/tags/1.3/docs/javadocs/index.html
1.简单的处理list和map
Gson gson = new Gson();
List testList = new ArrayList();
testList.add("first");
testList.add("second");
String listToJson = gson.toJson(testList);
System.out.println(listToJson);
//prints ["first","second"]
Map testMap = new HashMap();
testMap.put("id", "id.first");
testMap.put("name","name.second");
String mapToJson = gson.toJson(testMap);
System.out.println(mapToJson);
//prints {"id":"id.first","name":"name.second"}
2.处理带泛型的集合
List testBeanList = new ArrayList();
TestBean testBean = new TestBean();
testBean.setId("id");
testBean.setName("name");
testBeanList.add(testBean);
List testBeanList = new ArrayList();
TestBean testBean = new TestBean();
testBean.setId("id");
testBean.setName("name");
testBeanList.add(testBean);
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken>() {}.getType();
String beanListToJson = gson.toJson(testBeanList,type);
System.out.println(beanListToJson);
//prints [{"id":"id","name":"name"}]
List testBeanListFromJson = gson.fromJson(beanListToJson, type);
System.out.println(testBeanListFromJson);
//prints [TestBean@1ea5671[id=id,name=name,birthday=]]
分享到:
相关推荐
google json: gson
java开发中使用JSON和gson所需要的jar,里面包含了所有的jar,这样我们就可以随便使用JSON和gson了
JSON、GSON解析json数据,详细介绍了json和gson的使用,解析复杂json数据
Json解析,Gson解析,本地,网络解析简单的案例
能够便利拼接json字符串,方便程序员更加整理大型json。
谷歌处理JSON数据的jar包Gson.jar 这种处理方式不需要依赖别的jar包,而且使用方法非常简单: List<String> list = new ArrayList(); list.add("zhangsan"); list.add("lisi"); list.add("wangwu"); Gson ...
包含以下java源文件: ...com.google.gson.JsonNull.class com.google.gson.JsonObject.class com.google.gson.JsonParseException.class com.google.gson.JsonParser.class com.google.gson.JsonPrimitive.class ...
java解析json所需的三个jar包。包含org.json、Gson、fastjson三个jar包
json-gson-fastJson的jar包
json&gson;&gson;类型转换jar包
google-json-gson-master,google的jsonjar包,提供json方法。
dom4j-json-Gson完整jar包下载
android之json和gson数据解析最完整的代码例子(包括各种样式的json数据) 本人在2.3.3模拟器上已亲测可以运行。最适合学习json 或gson数据解析的初学者。
json包、gson包以及相应的源码,其中包括了json的工具类-json.jar以及gson的工具类-gson.jar,以及json-sources 和gson-sources。
Gson是Google的一个开源项目,可以将Java对象转换成JSON,也可能将JSON转换成Java对象。 Gson里最重要的对象有2个Gson 和 GsonBuilder Gson有2个最基本的方法 1) toJson() – 转换java 对象到JSON 2) from...
解放你的双手,不用再去一个一个JSONObject去处理解析问题,两行代码搞定一切
我们要使用json和java中使用,我们需要使用到一个第三方的包。它就是gson.jar。
网络数据解析包合集JSON、GSON&FASTJOSN
json和gson的jar包!
JSON GSON FASTJSON jar包,包括文档、源码和API。