我是JSON和JSP的新手。我正在编写一个REST url,它将从java swing应用程序中调用。 url将显示一些JSON数据。 下面是我的jsp代码:
JSONObject MainObject=null;
JSONObject json1=null;
JSONArray jarr=new JSONArray();
String email = request.getParameter("EMAIL");
cc = new MySQLConnection();
String query = "SELECT * FROM CLIENTS WHERE EMAIL = '"+email+"'";
try{
conn =cc.db();
stmtt =conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmtt.executeQuery(query);
if(rs.next()){
json1 = new JSONObject();
MainObject = new JSONObject();
json1.put("EMAIL", rs.getString("EMAIL"));
json1.put("ID", rs.getLong("ID"));
json1.put("COMPANY", rs.getString("COMPANY"));
json1.put("CODE", rs.getString("CODE"));
json1.put("VALID_TILL", rs.getLong("VALID_TILL"));
jarr.put(json1);
MainObject.put("SUCCESS", "1");
MainObject.put("CLIENTS", jarr);
}
//else{
//out.println("Error....");
//}
out.println(MainObject);
}
catch(SQLException ex){out.println(ex);}
finally{try{stmtt.close(); rs.close();conn.close();} catch(SQLException ex){out.println(ex);} }
这给了我以下输出:{"SUCCESS":"1","CLIENTS":[{"VALID_TILL":20,"COMPANY":"BOOK PALACE","ID":1,"EMAIL":"pranjal","CODE":"98877655"}]}
现在,当我尝试解析Swing应用程序中的数据时:它给了我错误。我正在解析这个:
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("EMAIL", mail)) ;
JSONObject json = jParser.makeHttpRequest(YOURUrl, "GET", params);
System.out.println(json.toString());
try {
int success = json.getInt("SUCCESS");
if (success == 1) {
products = json.getJSONArray("CLIENTS");
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String Company = c.getString("COMPANY");
String Name = c.getString("C_NAME");
String Email = c.getString("EMAIL");
String Code = c.getString("CODE");
String ValidTill = c.getString("VALID_TILL");
String sl = c.getString("ID");
COMPANY = Company;
CNAME = Name;
EMAIL = Email;
CODE = Code;
VALID_TILL = ValidTill;
SL = sl;
}
}
} catch (JSONException e) {
System.err.println(e);
}
我的jsp文件中有什么错误?我在哪里做错了?