package com.wangyan.test;
import com.google.gson.Gson;
public class JsonTest {
public static void main(String[] args) {
Student student =new Student();
student.setName("王梦雪");
student.setAge(11);
student.setSex("女");
student.setSalary(1000.3);
Gson gson=new Gson();
//把java对象转成json
String string= gson.toJson(student).toString();
System.out.println(string);
//把json转为java对象
Student student2= gson.fromJson(string, Student.class);
System.out.println(student2.toString());
}
}
</pre>第一次输出的结果是把java对象转json输出的结果为:{"name":"王梦雪","age":11,"sex":"女","salary":1000.3}
第二次输出的结果是把json转为Java对象输出的结果为:Student [name=王梦雪, age=11, sex=女, salary=1000.3]