How to include and use GSON library in Eclipse project
I am on a PHP background so I apologize for my absence. I have to use JSON in one of my projects and I cannot, for the rest of my life, figure out how to import and use the GSON library.
I've followed adding a library to Eclipse and using GSON , but for some reason my code doesn't work.
The goal is to pass the object ArrayList
back as a JSON array so that I can use JQuery (inside the Ajax success function) to iterate over it.
But when I use the following (it's not class
, just a jsp file that connects to the database, fetches some information and stores it in ArrayList
):
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%
String kw = request.getParameter("key");
try {
java.sql.Connection con;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/abcd", "root", "pass");
st = con.createStatement();
rs = st.executeQuery("SELECT DISTINCT t.tckid FROM ticket t WHERE t.tckid LIKE '%"+kw+"%'");
ArrayList<String> tickets = new ArrayList<String>();
while(rs.next()) {
String TCKTID = rs.getString(1);
tickets.add(TCKTID);
}
rs.close();
st.close();
con.close();
Gson gson = new Gson(); // this is giving me Gson cannot be resolved to a type
So what I can gather, the class Gson
was not imported at all. Is there a way to check if the library was imported successfully? Or do I also need to use some code import ***
at the top of the file?
source to share
The problem is that you are not importing the Gson package into the current one JSP
.
<%
Gson gson=new Gson();
%>
import to JSP
<%@ page import="com.google.gson.Gson" %>
but remember to use scripts in the JSP
MVC pattern to separate the server side from the client side actions / goals, if you want to display values ββcoming from the database you can always use JSTL and use different scopes (request scope, session scope, etc. .).
source to share