我想将java servlet方法创建为Create Update或Delete Json file.In json文件是关于Dog类的信息。 这里是来源,还没有完成 这是狗类
public class Dog {
private String name;
private int age;
private int height;
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 getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
这是CreateServlet。
import com.google.gson.Gson;
import javax.servlet.http.HttpServlet;
import java.io.FileWriter;
public class CreateServlet extends HttpServlet {
public static void main(String[] args) {
Dog dog = new Dog();
dog.setAge(20);
dog.setHeight(15);
dog.setName("name");
Gson gson = new Gson();
try {
FileWriter writer = new FileWriter("C://tojson.json");
gson.toJson(dog, writer);
} catch (Exception exp) {
System.out.println(exp.getMessage());
}
}
}
如何处理Servlet方法的CRUD json文件?谢谢