mirror of
https://github.com/Estom/notes.git
synced 2026-02-04 11:04:21 +08:00
Java内容重新整理删除过期的东西
This commit is contained in:
54
Java/JavaDemo/codedemo/json/gson/CollectionsSerialize.java
Normal file
54
Java/JavaDemo/codedemo/json/gson/CollectionsSerialize.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package cn.aofeng.demo.json.gson;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
/**
|
||||
* 集合的序列化。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class CollectionsSerialize {
|
||||
|
||||
public void serialize(Collection<?> c) {
|
||||
Gson gson = new Gson();
|
||||
System.out.println( gson.toJson(c) );
|
||||
}
|
||||
|
||||
public void serialize(Map<?, ?> map) {
|
||||
Gson gson = new Gson();
|
||||
System.out.println( gson.toJson(map) );
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
CollectionsSerialize cs = new CollectionsSerialize();
|
||||
|
||||
// 整型List
|
||||
List<Integer> intList = new ArrayList<Integer>();
|
||||
intList.add(9);
|
||||
intList.add(8);
|
||||
intList.add(0);
|
||||
cs.serialize(intList);
|
||||
|
||||
// 字符串Set
|
||||
Set<String> strSet = new HashSet<String>();
|
||||
strSet.add("Hello");
|
||||
strSet.add("World");
|
||||
strSet.add("Best");
|
||||
cs.serialize(strSet);
|
||||
|
||||
// Map
|
||||
Map<String, Person> objMap = new HashMap<String, Person>();
|
||||
objMap.put("marry", new Person("马丽", 20));
|
||||
objMap.put("xiaomin", new Person("小明", 21));
|
||||
cs.serialize(objMap);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user