You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hunjianghu/WeChat/GetJson.java

39 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package WeChat;
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.ServerException;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Map;
public class GetJson {
public static void Getinfo(HttpServletRequest request, HttpServletResponse response, String QueryString, Map<String,String> names) throws ServerException, IOException {
response.setHeader("Content - Encoding","utf-8");
response.setContentType("text/json; charset=utf-8");
ServletContext context = request.getServletContext();
PrintWriter out = response.getWriter();
Statement state = (Statement)context.getAttribute("state");
JSONArray jsonArray = new JSONArray();
try{
ResultSet rs=state.executeQuery(QueryString);
while(rs.next()){
JSONObject jsonObject = new JSONObject();
for (Map.Entry<String, String> entry : names.entrySet()) {
jsonObject.put(entry.getKey(), rs.getString(entry.getValue()));
}
//对于图片的话我们直接返回对应的图书封面的url就可以了
jsonArray.add(jsonObject);
}
}catch (Exception e){
e.printStackTrace();
}
out.println(jsonArray);
}
}