NutzCN Logo
问答 前端List<Map>传给后端,一直报String不能转成Map
发布于 2181天前 作者 qq_4ad560c4 1892 次浏览 复制 上一个帖子 下一个帖子
标签:

求解。。。。。。。。。。。。。。。

23 回复

Json.fromJsonAsList()要放个Reader,这我不知道该放啥

把适配器那个章节看几遍

加了适配也不行啊

@AdaptBy(type = JsonAdaptor.class)
    public Object assignRfidDo(@Param("toolsId")String toolsId,@Param("list")List<Map<String,String>> listStr,HttpServletRequest req){
            System.out.println( listStr);
     }

后台listStr打印出来是:[[{},{}]]

前端的list打印出来是:

(2) [Map(4), Map(4)]
0: Map(4) {"Store0" => "09f9a228d4144ca88dfad836bc26b30f", "Cab0" => "ec735e5f8d854d07818161c617990828", "Space0" => "bebd844438db479581887cdf8f696082", "Number0" => "1"}
1: Map(4) {"Store1" => "953047271cd24a9db42c2d927444e486", "Cab1" => "8fabf25af731453d8966f591298895f3", "Space1" => "4e1dd2bc7425440b94f3795a78474fcc", "Number1" => "1"}
length: 2

前端代码怎么写的

不用JSON.stringify(),直接传list也不行

$.post("${base}/plat/cab/assignDo", {
            list:JSON.stringify(list),
            toolsId: "${obj.id}"
        }, function (data) {
            if (data.code == 0) {
                Toast.success(data.msg);
                datatable.ajax.reload();
                datatable_rfids.ajax.reload();
            } else {
                Toast.error(data.msg);
            }
            $('#assignToolsPanel').modal('hide');
        }, "json");

要整个body都是json字符串

      $.post("${base}/plat/cab/assignDo", JSON.stringify({list:list,toolsId: "${obj.id}"}), function (data) {
            if (data.code == 0) {
                Toast.success(data.msg);
                datatable.ajax.reload();
                datatable_rfids.ajax.reload();
            } else {
                Toast.error(data.msg);
            }
            $('#assignToolsPanel').modal('hide');
        }, "json");

改了还是不行,后台现在接收到的是:

[{"list":[{},{}],"toolsId":"3fcb27c7eef94a58b746d5a3c67f1a9e"}]

前端把JSON.stringify的结果打印看看是什么

这什么情况,list不是空的啊

JSON.stringify()解析不了?

list怎么来的?

var list = new Array();
        for (var a = 0; a < window.rows; a++) {
            var map = new Map();
            map.set("Store"+a,$("#Store"+a).find("option:selected").val());
            map.set("Cab"+a,$("#Cab"+a).find("option:selected").val());
            map.set("Space"+a,$("#Space"+a).find("option:selected").val());
            map.set("Number"+a,$("#Number"+a).val());
            list.push(map);
        }

es6的map...

你还是用 var map = {} 得了

var map = {} 的话。。。页面直接报错了

Uncaught TypeError: map.set is not a function
    at save (eval at <anonymous> (jquery-1.11.1.min.js:2), <anonymous>:102:17)
    at HTMLAnchorElement.onclick (tools:1)

map["xxx"] =xxx

十分感谢!!!用这个map就好了,还问一下就是后端接收的list里又套了一个list,这是怎么回事?

[[{"Store0":"09f9a228d4144ca88dfad836bc26b30f","Cab0":"ec735e5f8d854d07818161c617990828","Space0":"bebd844438db479581887cdf8f696082","Number0":"1"},{"Store1":"09f9a228d4144ca88dfad836bc26b30f","Cab1":"ec735e5f8d854d07818161c617990828","Space1":"bebd844438db479581887cdf8f696082","Number1":"1"}]]

前端打印则是正常的

[{"Store0":"09f9a228d4144ca88dfad836bc26b30f","Cab0":"ec735e5f8d854d07818161c617990828","Space0":"bebd844438db479581887cdf8f696082","Number0":"1"},{"Store1":"09f9a228d4144ca88dfad836bc26b30f","Cab1":"ec735e5f8d854d07818161c617990828","Space1":"bebd844438db479581887cdf8f696082","Number1":"1"}]

如何打印的

System.out.println( listStr);
System.out.println(Json.toJson( listStr));
添加回复
请先登陆
回到顶部