我正在使用JSon-RPC library .
Servlet:
我想输入List<Student>
进入JSonArray
与 {["name":"AAA","age":"24"]["name":"BBB","age":"12"]}
相同。
JsonArray
有一个接受 Collection
的构造函数与参数相同。
如果结果是 JsonObject
然后我会用方法out.print(instanceJsonObject.toString())
回复客户。
现在我不知道怎么回复JsonArray
从服务器到客户端。
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("Vu Duc Hoan", "C1010G", "24"));
list.add(new Student("Vu Duc Hoan2", "C1010G2", "242"));
JSONObject js = new JSONObject();
org.json.JSONArray array = new JSONArray(list);
客户:
能告诉我如何获取JsonArray中的数据吗?我正在使用$.getJson
btnJson.click(function(){
$.getJSON("../DemoAjax/Controller?action=getJson",function(data){
});
最佳答案
我认为这是您正在寻找的servlet代码,但我认为您需要首先将Student
对象转换为JSONObject
,然后将
,此 tutorial可能对你有帮助:JSONArray
中的 >JSONObject
JSONObject js = new JSONObject();
org.json.JSONArray jsonArray = new JSONArray(list);
// set the response content-type
response.setContentType("application/json");
PrintWriter out = response.getwriter();
// writing the json-array to the output stream
out.print(jsonArray);
out.flush();
在 javascript 方法中,function(data)
中的 data
将包含此 json 数组,您可以使用此 answer获取 html 页面中的数据。
关于jsp - JSP 中的 Ajax jquery 与 JsonArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13216037/