I’m using JSon-RPC library.
Servlet:
I want to put List<Student> into JSonArray that is the same as {["name":"AAA","age":"24"]["name":"BBB","age":"12"]}.
JsonArray have a contructor that accepts a Collection being the same as parameter.
If result is a JsonObject then I’ll response to client with method out.print(instanceJsonObject.toString()).
Now I don’t know how to response JsonArray from Server to Client.
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);
Client:
Can you tell me how to get data in JsonArray? I’m using $.getJson
btnJson.click(function(){
$.getJSON("../DemoAjax/Controller?action=getJson",function(data){
});
I think this is the servlet code you are looking for, but I think you would need to first convert the
Studentobject to theJSONObjectand then put theJSONObjects inJSONArray, this tutorial might help you:In the javascript method the
datainfunction(data)would contain this json-array and you can use this answer to get the data in your html page.