【问题标题】:Ajax jquery with JsonArray in JSPJSP中带有JsonArray的Ajax jquery
【发布时间】:2012-11-05 18:36:15
【问题描述】:

我正在使用JSon-RPC library

Servlet:

我想将List<Student> 放入与{["name":"AAA","age":"24"]["name":"BBB","age":"12"]} 相同的JSonArrayJsonArray 有一个构造函数,它接受与参数相同的 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){
});

【问题讨论】:

    标签: jsp jquery arrays


    【解决方案1】:

    我认为这是您正在寻找的 servlet 代码,但我认为您需要先将Student 对象转换为JSONObject,然后将JSONObjects 放入JSONArray,即@987654321 @ 可能会帮助你:

    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 页面中的数据。

    【讨论】:

      猜你喜欢
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-26
      相关资源
      最近更新 更多