Json解析
1.Gson解析 对象
import com.google.gson.Gson;
public class Demo1Gson {
public static void main(String[] args) {
//.1创建Gson对象
//Gson g = new Gson();
//2.转换
Book b = new Book("100", "金苹果", "种植苹果真辛苦");
String s = new Gson().toJson(b);
System.out.println(s);
}
}
2.转换Json
import com.google.gson.Gson;
public class Demo2 {
public static void main(String[] args) {
//.1创建Gson对象
Gson g = new Gson();
//2.转换 {"id":"100","name":"金苹果","info":"种植苹果真辛苦"}
Book b = g.fromJson("{\"id\":\"100\",\"name\":\"金苹果\",\"info\":\"种植苹果真辛苦\"}",Book.class);
System.out.println(b.getName());
}
}
注意:使用Gson 方法前需导入jar包