转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=]]
在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=]]
本文介绍了GSON库的基本用法,包括如何将Java对象转换为JSON字符串,以及如何将JSON字符串转换回Java对象。特别关注了处理列表和映射等数据结构的方法,并展示了如何处理带有泛型的集合。
582

被折叠的 条评论
为什么被折叠?



