关于JsonArray与JsonObject的使用
学习地址:http://blog.csdn.net/lishuangzhe7047/article/details/28880009
关于前台向后台传递数组(里面包含json格式)
- [{"id":1,"name":"同学1"},{"id":2,"name":"同学2"}]
前台代码:
- var datalist= new Array();
- for ( var i = 1; i <= bookCount; i++) {
- var object = new Object();
- object.id = i;
- object.name = "同学"+i;
- datalist.push(object);
- }
- function saveData(datalist){
- .ajax({
- type:"POST",
- url:base +"/save",
- data:{"datalist":JSON.stringify(datalist)},
- dataType: "json",
- success:function (data){
- },
- error:function (){
- alert("insert serv wrong");
- }
- );
- }
- 后台解析:
- public Object save(@Param("datalist")String list){
- JSONArray jsonArray = new JSONArray(list);
- String id = jsonArray.getJSONObject(0).get("id");</span>//得到第一个同学的id
- String name = jsonArray.getJSONObject(0).get("name");</span>//得到第一个同学的name
- return true;