2017-03-07 10 views
0

ボタンクリックでajax呼び出しを行いたい。これはmysqlからレコードを取得し、従業員オブジェクトに入れます。今私は私のajax呼び出しに戻って、従業員のオブジェクトの配列の種類のものを送信したい。 JSONの使用を考えました。 package com;このコードは正常に動作していますが、私はEmployeeクラスとそのオブジェクトを使用できません。ここで私はJSONとその動作を直接使用しました。JavaオブジェクトをJsonオブジェクトに変換してjson配列に入れる方法

import java.sql.Connection; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
public class SelectUsers { 
    public JSONArray selectQueryDemo() throws SQLException 
    { 
     GlobalConnection gc=new GlobalConnection(); 
     Connection conn=gc.getConnection(); 
     Statement s=conn.createStatement(); 
     ResultSet result=s.executeQuery("select * from tblemployees"); 
     String name = ""; 
     JSONArray jsonArray = new JSONArray(); 
     while(result.next()) 
     { 
      JSONObject obj = new JSONObject(); 
      try { 
       obj.put("id", result.getInt(1)); 
       obj.put("name", result.getString(2)); 
       obj.put("gender", result.getString(3)); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      jsonArray.put(obj); 
     } 
     return jsonArray; 
    } 
} 

答えて

0

JSonオブジェクトをEmployeeオブジェクトでインスタンス化することで、これを実行できるはずです。

公共JSONObject(オブジェクト・ビーン)

ビーンゲッターを使用してオブジェクトからJSONObjectを構築。

Employeeオブジェクトにゲッターがありますか?

関連する問題