• ajax与json总结


    1.jquery中调用ajax方法

    $.ajax({
      async:true,
      type:"post",
      url:"xxxServlet",
      data:{"account":"jack","pwd":"123"},
      success:function(data){
          //dowork
      },
      //error:function(){alert("error!");},
      dataType:"json"
    });
    注:指定dataType为json时,不要用var obj = eval("("+data+")");去再次解析,会报错
    

    2.json格式

    1.json对象
      {key:value,key:value}
      {"name":"jack","sex":"男","age":"25"}
    2.json数组或集合格式
      [{key:value,key:value},{key:value,key:value}]
      [{"name":"jack","sex":"男","age":"25"},     {"name":"tom","sex":"男","age":"26"}]
    3.json对象,数组嵌套
      {  
            "param":[{key:value,key:value},{key:value,key:value}] 
      }
      {
    	"param1":[{key:value,key:value},{key:value,key:value}],
    	"param2":[{key:value,key:value},{key:value,key:value}],
    	"param3":[{key:value,key:value},{key:value,key:value}]
      }
      {
    	 "param1":"value1",
    	 "param2":{},
    	 "param3":[{key:value,key:value},{key:value,key:value}]
      }
    

    3.谷歌的gson包

      Gson gson = new Gson();
      String json = gson.toJson(car);
      resp.getWriter().write(json);
    

    4.阿里的fastjson包

    JSON-jsonString(toJOSNString) 
    JSONObject-map集合(put) 
    JSONArray-list集合(add)
    

    4.1.响应jsonString

    String jsonString = JSON.toJSONString(car);
    resp.getWriter().write(jsonString);
    

    4.2.响应json对象

    JSONObject data=JSONObject.parseObject(JSON.toJSONString(car));
    resp.getWriter().print(data);
    

    4.3.JSONObject对象

    JSONObject jo = new JSONObject();
    jo.put("message", "用户名可注册!");
    resp.getWriter().print(jo);
    

    4.4.JSONArray数组

    JSONObject jo1 = new JSONObject();
    JSONObject jo2 = new JSONObject();
    JSONArray ja = new JSONArray();
    ja.add(jo1);
    ja.add(jo2);
    resp.getWriter().println(ja);
    
  • 相关阅读:
    Qt double转换成Qstring
    QT 控件ComboBox
    前端web项目打包(二)
    前端Web打包成可执行程序
    div中下拉框无法点击展开跟选中
    div下多个table并排排列
    VS将数据保存在excel表格中
    关于windows下任务栏应用程序标签消失问题的解决办法
    Java8中map()和flatMap()的区别
    Java对象的深拷贝
  • 原文地址:https://www.cnblogs.com/itzlg/p/11318799.html
Copyright © 2020-2023  润新知