JSON传参

通过javascript将数据组织成json格式,然后传到java后台。

注意:前台json数组传参到后台时候需要将对象(json或json数组)转换成字符串(字符串数组)。

Simple:

1、前台:
var jsonArray = [];
var jsonObject = {};
var array = [];
jsonObject.push(array); //json对象里面的数组
jsonArray.push(JSON.stringify(jsonObject));
var param = "["+jsonArray+"]";

数据示例:[{"name":"张三","sex":"男","family":["父亲","母亲","女儿"]},{"name":"张三","sex":"男","family":["父亲","母亲","儿子","爷爷"]},{}]]

 

2、后台

String results = getPara("results");

JSONArray array = JSONArray.fromObject(results);
List<ObjectVo> list = JSONArray.toList(array, ObjectVo.class); 

 

3、JSONArray.toList方法过时,可以用JSONArray.toCollection

public static void main(String[] args) {
System.out.println("come in ...");
String arr = "[{a:1},{b:2},{c:3}]";
JSONArray ja = JSONArray.fromObject(arr);
List<Integer> list = (List<Integer>)JSONArray.toCollection(ja);
for(Object obj : list){
JSONObject jb = JSONObject.fromObject(obj);
for(String s : (Set<String>)jb.keySet()){
System.out.println(jb.get(s));
}
}
System.out.println("size:"+list.size());
}

 

4、后台可以传List,也可以传JsonArray到前台

public static void main(String[] args) {
System.out.println("come in ...");
List list = new ArrayList();
list.add(1);
list.add(5);
list.add(7);
JSONArray array = new JSONArray();
JSONArray workList = array.fromObject(list);
System.out.println(workList);
}

 

posted @ 2017-02-24 17:44  N神3  阅读(2876)  评论(0编辑  收藏  举报