java对象转json与json转对象
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.json.JSONObject;
import java.util.Arrays;
public class PojoToJSON {
public static void main(String[] args) throws JsonProcessingException {
Student student = new Student();
student.setId(1);
student.setName("A");
student.setAge(20);
JSONObject object = new JSONObject(student);
String json = object.toString();
System.out.println(json);
System.out.println(new Gson().toJson(student));
Student student2 = JSONObject.toJavaObject(object,Student.class);
}
}