jsp - JSP 中的 Ajax jquery 与 JsonArray

标签 jsp jquery arrays

我正在使用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,然后将 JSONArray 中的 >JSONObject,此 tutorial可能对你有帮助:

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/

相关文章:

javascript - 仅捕获链接上的左键单击

javascript - 仅允许电子邮件列表使用表单 javascript 验证提交请求

java - 根据用户输入测试字符串和整数数组

java - 使用 struts 中的 jsp 唯一标识每一行以在表单中使用

java - Spring MVC 资源映射

java - 从链接中的 servlet 发送值到 jsp 页面

java - Spring MVC - 如何将模型属性传递给多个 JSP

javascript - CSS slide 不滑动,只是出现

jquery 每次循环遍历具有多个对象的数组

C程序查找数字序列中缺失的整数