ajax与json

匿名 (未验证) 提交于 2019-12-02 23:53:01
$.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+")");去再次解析,会报错
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}]   }
  Gson gson = new Gson();   String json = gson.toJson(car);   resp.getWriter().write(json);
JSON-jsonString(toJOSNString)  JSONObject-map集合(put)  JSONArray-list集合(add)
String jsonString = JSON.toJSONString(car); resp.getWriter().write(jsonString);
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);
JSONObject jo1 = new JSONObject(); JSONObject jo2 = new JSONObject(); JSONArray ja = new JSONArray(); ja.add(jo1); ja.add(jo2); resp.getWriter().println(ja);

来源: https://www.cnblogs.com/itzlg/p/11318799.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!