这是处理json的纯手写json返回信息处理
/**
* "处理json"
*/
public class ManagerJson{
//json处理类
public static String JSon(HttpServletRequest req, HttpServletResponse resp){
List<String>list= ManagerJson.GetAll(req, resp);//调用集合
StringBuilder jsonFormat=new StringBuilder("{\"data\":\"");
int listLastIndex=list.size()-1;//最后一个的下标
for (int i=0;i<list.size();i++) {
if (i==listLastIndex)//如果是最后一个的话不加逗号
jsonFormat.append(list.get(i));
else
jsonFormat.append(list.get(i)).append(",");
}
return String.valueOf(jsonFormat.append("\"}"));//返回json
}
//用户信息处理集合类
public static List<String> GetAll(HttpServletRequest req, HttpServletResponse resp)
{
String[]users={"userid","username","userpassword","userregisterTime","userEmail","userCall","userSex"};//记录用户信息
List<String>List=new ArrayList<>();
String username=req.getParameter("username");
ResultSet rs=GavinKaoDBUtil.Check(username);//这个是数据库的查询方法,不会写自行百度
try {
if (rs.next())
{
for (String user:users)List.add(rs.getString(user));
}
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return List;
}
}