jQuery的阿贾克斯与JsonArray在JSP [英] Ajax jquery with JsonArray in JSP
问题描述
我使用 JSON-RPC库。
的Servlet:
我想把列表<学生>
到 JSonArray
这是一样的 {[名:AAA,年龄:24] [名:BBB,时代:12]}
。 JsonArray
有一个接受收藏
是相同的参数构造器。
如果结果是一个的JSONObject
然后我会响应客户端的方法的out.print(instanceJsonObject.toString())
。
现在我不知道该怎么回应 JsonArray
从服务器到客户端。
的ArrayList<的Student GT;名单=新的ArrayList<的Student GT;();
list.add(新学生(武德的还,C1010G,24));
list.add(新学生(武德Hoan2,C1010G2,242));JS的JSONObject =新的JSONObject();
org.json.JSONArray阵列=新JSONArray(名单);
客户:
你能告诉我怎么去在JsonArray数据?我使用 $。的getJSON
btnJson.click(函数(){
$ .getJSON(../ DemoAjax /控制器?行动=的getJSON功能(数据){
});
我觉得这是servlet code你正在寻找,但我认为你需要先转换成学生
对象的的JSONObject
然后把的JSONObject
S IN JSONArray
,这的教程可以帮助你:
的JSONObject JS =新的JSONObject();
org.json.JSONArray jsonArray =新JSONArray(名单);//设置响应的内容类型
response.setContentType(应用/ JSON);PrintWriter的OUT = response.getwriter();//写JSON数组输出流
的out.print(JSONArray),其中,
了out.flush();
在JavaScript方法在功能(数据)
将包含此JSON阵列,您可以使用数据
这回答来获得你的HTML页面中的数据。
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 Student
object to the JSONObject
and then put the JSONObject
s in JSONArray
, this tutorial might help you:
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();
In the javascript method the data
in function(data)
would contain this json-array and you can use this answer to get the data in your html page.
这篇关于jQuery的阿贾克斯与JsonArray在JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!