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));


    }
}