Java對象與Json字元串的轉換
Java對象與Json字元串的轉換
JSON是一種輕量級的數據交換格式,常用於前後端的數據交流
後端 : 前端
Java對象 > JsonString
Java對象 < jsonString
使用Gson進行驗證
Bean對象 Java解析Bean對象時需要將對應的’類對象’傳入
package com.json.bean;
public class Person {
private int age;
private String name;
public Person() {
}
public Person(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.json.bean;
import com.google.gson.Gson;
import org.junit.Test;
public class BeanTrans {
Gson gson = new Gson();
@Test
public void test(){
Person p = new Person(1, "帥哥");
String bean2json= gson.toJson(p);
System.out.println(bean2json);
Person p1 = gson.fromJson(bean2json,Person.class);
System.out.println(p1);
System.out.println(p1.getName());
}
}
{"age":1,"name":"帥哥"}
Person{age=1, name='帥哥'}
帥哥
List對象 List需要新建一個類繼承TypeToken,然後在Java解析字元串時將TypeToken的子類的『類對象』傳入
package com.json.list;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
public class PersonList extends TypeToken<ArrayList<Person2>> {
}
package com.json.list;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class ListTrans {
Gson gson = new Gson();
List<Person2> list = new ArrayList<Person2>();
@Test
public void test(){
Person2 p1 = new Person2(1, "我好帥");
Person2 p2 = new Person2(2, "你也好帥");
list.add(p1);
list.add(p2);
String list2json = gson.toJson(list);
System.out.println(list2json);
ArrayList<Person2> list1 = gson.fromJson(list2json,new PersonList().getType());
System.out.println(list1);
System.out.println(list1.get(0).getName());
}
@Test
public void test1(){
Person2 p3 = new Person2(3, "我好帥");
Person2 p4 = new Person2(4, "你也好帥");
list.add(p3);
list.add(p4);
String list2json = gson.toJson(list);
System.out.println(list2json);
// 匿名內部類實現
ArrayList<Person2> list1 = gson.fromJson(list2json,new TypeToken<ArrayList<Person2>>(){}.getType());
System.out.println(list1);
System.out.println(list1.get(0).getName());
}
}
Map對象 Java解析Map對象時需要先實現一個繼承TypeToken的子類,然後將對應的子類’類對象’傳入
package com.json.map;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.Hashtable;
public class PersonMap extends TypeToken<Hashtable<Integer, Person3>> {
}
package com.json.map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.junit.Test;
import java.util.*;
public class MapTrans {
Gson gson = new Gson();
Map<Integer,Person3> map = new Hashtable<Integer, Person3>();
@Test
public void test(){
Person3 p5 = new Person3(1, "我好帥");
Person3 p6 = new Person3(2, "你也好帥");
map.put(1,p5);
map.put(2,p6);
String list2json = gson.toJson(map);
System.out.println(list2json);
Map<Integer,Person3> list1 = gson.fromJson(list2json,new PersonMap().getType());
System.out.println(list1);
System.out.println(list1.get(1));
}
@Test
public void test1(){
Person3 p5 = new Person3(3, "我好帥");
Person3 p6 = new Person3(4, "你也好帥");
map.put(1,p5);
map.put(2,p6);
String map2json = gson.toJson(map);
System.out.println(map2json);
// 匿名內部類實現
Map<Integer,Person3> map2 = gson.fromJson(map2json,new TypeToken<Hashtable<Integer, Person3>>(){}.getType());
System.out.println(map2);
System.out.println(map2.get(1));
}
}