简述:
testJson_1中 将不同的java对象转换为json格式文件(String),
testJson_2中传入testJson_1中生成的JSON String 参数,反向返回各种Java的对象
包括:java的String、List、HashMap、JavaBean, 以及对象内包含list成员的对象转为Json String 并且 Json转回
注意点:
Gson的toJson() ,fromJson()函数使用,和两个函数所要求的参数
使用gson 1.7.1包(导入方法是用ivy工具-可参考配置里的一篇记录,或者直接加入装jar包)
代码:
1) Java Object To JSON
TestJson_1.java
package testJson; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.google.gson.*; class PersonInfo{ PersonInfo(String name,int age){ this.name = name; this.age = age; } public PersonInfo() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } private String name; private int age; } class FriendsBook{ FriendsBook(String ownerName,List<PersonInfo> friendsBook){ this.ownerName = ownerName; this.friendsBook = friendsBook; } public String getOwnerName() { return ownerName; } public void setOwnerName(String ownerName) { this.ownerName = ownerName; } public List<PersonInfo> getFriendsBook() { return friendsBook; } public void setFriendsBook(List<PersonInfo> friendsBook) { this.friendsBook = friendsBook; } private String ownerName; private List<PersonInfo> friendsBook; } //define a PersonBean to test Bean to Json class PersonBean{ PersonBean(int id,String name,int age){ setId(id); setName(name); setAge(age); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } private int id; private String name; private int age; private ArrayList<String>friendsList; } /** * * Java Object TO JSON String * */ public class TestJson_1 { //use Gson lib private static Gson gson = new Gson(); public String GetStringToJson(){ String jsonResult = gson.toJson(new String("aaa")); return jsonResult; } public String GetArrayListToJson(){ ArrayList<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); String jsonResult = gson.toJson(list); return jsonResult; } public String GetHashMapToJson(){ Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "aaa"); map.put(2, "bbb"); map.put(3, "ccc"); String jsonResult = gson.toJson(map); return jsonResult; } public String GetPersonBeanToJson(){ PersonBean pbean = new PersonBean(0,"aaa",21); String jsonResult = gson.toJson(pbean); return jsonResult; } public String GetFriendsBookToJson(){ PersonInfo p0 = new PersonInfo("John",22); PersonInfo p1 = new PersonInfo("Marry",24); PersonInfo p2 = new PersonInfo("Hellon",21); List<PersonInfo> friendsList = new ArrayList<PersonInfo>(); friendsList.add(p0); friendsList.add(p1); friendsList.add(p2); FriendsBook friendsBook = new FriendsBook("OWNER",friendsList); String jsonResult = gson.toJson(friendsBook); return jsonResult; } }
2) JSON To Java Object
TestJson_2.java
package testJson; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; //JSON String To Java Object public class TestJson_2 { private static Gson gson = new Gson(); public String GetJsonToString(String jsonString){ String str = gson.fromJson(jsonString, String.class); return str; } public ArrayList<String> GetJsonToArrayList(String jsonString){ ArrayList<String> list = gson.fromJson(jsonString, new TypeToken<ArrayList<String>>(){}.getType()); return list; } public Map<Integer, String> GetJsonToHashMap(String jsonString){ HashMap<Integer,String> hashMap = gson.fromJson(jsonString, new TypeToken<HashMap<Integer,String>>(){}.getType()); return hashMap; } public PersonBean GetJsonToPersonBean(String jsonString){ PersonBean personBeanArrayList = gson.fromJson(jsonString, PersonBean.class); return personBeanArrayList; } public FriendsBook GetJsonToFriendsBook(String jsonString){ FriendsBook friendsBook = gson.fromJson(jsonString, FriendsBook.class); return friendsBook; } }
TestJson.Java
package testJson; public class TestJson { private static final TestJson_1 tj1 = new TestJson_1(); private static final TestJson_2 tj2 = new TestJson_2(); public static void main(String[] args){ //Java Object to JSON String System.out.println("TestStringToJson: " + tj1.GetStringToJson() + "\n"); System.out.println("TestArrayListToJson: " + tj1.GetArrayListToJson() + "\n"); System.out.println("TestHashMapToJson: " + tj1.GetHashMapToJson() + "\n"); System.out.println("TestPersonBeanToJson: " + tj1.GetPersonBeanToJson() + "\n"); System.out.println("TestFriendsBookToJson: \n" + tj1.GetFriendsBookToJson() + "\n"); /* * reverse JSON String To Java Object */ System.out.println("/***********************JSON String To Java Object******************************************/\n"); String jsonToString = tj1.GetStringToJson(); System.out.println("Json To String: " + tj2.GetJsonToString(jsonToString) + "\n"); String jsonToArrayList_String_ = tj1.GetArrayListToJson(); System.out.println("Json To ArrayList----list[0]: " + tj2.GetJsonToArrayList(jsonToArrayList_String_).get(0) + "\n"); String jsonToHashMap = tj1.GetHashMapToJson(); System.out.println("Json To HashMap----hashMap.get(3): " + tj2.GetJsonToHashMap(jsonToHashMap).get(3) + "\n"); String jsonToPersonBean = tj1.GetPersonBeanToJson(); System.out.println("Json To PersonBean----personBean.getName(): " + tj2.GetJsonToPersonBean(jsonToPersonBean).getName() + "\n"); String jsonToFriendsBook = tj1.GetFriendsBookToJson(); System.out.println("Json To FriendsBook----friendsBook.getFriendsBook().get(0).getName(): " + tj2.GetJsonToFriendsBook(jsonToFriendsBook).getFriendsBook().get(0).getName() + "\n"); } }