2

JSon-RPCライブラリを使用しています。

サーブレット:

入れたいList<Student>JSonArrayはと同じ{["name":"AAA","age":"24"]["name":"BBB","age":"12"]}です。 パラメータと同じでJsonArrayあることを受け入れるコンストラクタを持っています。Collection結果がJsonObjectthenの場合、メソッドを使用してクライアントに応答します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){
});
4

1 に答える 1

2

これはあなたが探しているサーブレットコードだと思いますが、最初にStudentオブジェクトをに変換してからsをJSONObject入れる必要があると思います。このチュートリアルは、次のことに役立ちます。JSONObjectJSONArray

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メソッドでは、datainfunction(data)にこのjson-arrayが含まれ、この回答を使用してhtmlページのデータを取得できます。

于 2012-11-05T10:36:15.147 に答える