[英]How to convert Java object to Json object and put in json array
I want to do a ajax call on a button click. 我想在单击按钮时进行Ajax调用。 This will fetch records from mysql and will put in employee object. 这将从mysql获取记录,并将其放入employee对象。 Now i want to send the employee object array kind of thing back to my ajax call. 现在我想将员工对象数组类型的东西发送回我的ajax调用。 So thought of using JSON. 因此想到了使用JSON。 package com;This code is working fine but i am unable to use Employee class and its object.Here i directly used JSON and its working. package com;此代码工作正常,但我无法使用Employee类及其对象。在这里,我直接使用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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.