jquery-json使用

异步调用json的使用

 

乱码处理:

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("application/json ");
  response.setCharacterEncoding("utf-8");
  PrintWriter out = response.getWriter();
  
  String nameValue = "an&%\\&\"$#t";
  String jsonName = "results";
  StringBuilder json = new StringBuilder();
  json.append("{\""+jsonName+"\":[");
  
  for(int i=0;i<500;i++)
  {
   json.append("{");
   json.append("\"id\":\"1"+i+"\",\"name\":\"ant阿斯顿"+i+"\"");
   json.append("},");
  }
  json.append("{");
  json.append("\"id\":\"1\",\"name\":\""+string2Json(nameValue)+"\"");
  json.append("}");
  
  json.append("]}");
  out.write(json.toString());
 }
 
  public String string2Json(String s) {        
         StringBuffer sb = new StringBuffer();        
         for (int i=0; i<s.length(); i++) {  
             char c = s.charAt(i);    
              switch (c){  
              case '\"':        
                  sb.append("\\\"");        
                  break;        
              case '\\':        
                  sb.append("\\\\");        
                  break;        
              case '/':        
                  sb.append("\\/");        
                  break;        
              case '\b':        
                  sb.append("\\b");        
                  break;        
              case '\f':        
                  sb.append("\\f");        
                  break;        
              case '\n':        
                  sb.append("\\n");        
                  break;        
              case '\r':        
                  sb.append("\\r");        
                  break;        
              case '\t':        
                  sb.append("\\t");        
                  break;        
              default:        
                  sb.append(c);     
              }  
          }      
         return sb.toString();     
         }
}

你可能感兴趣的:(json jquery)