将对象数组发送到node / Parse到Json

时间:2018-04-29 12:09:26

标签: javascript json

我正在尝试将对象数组发送到节点。 如果我发送的没有stringify,我会得到一个与我发送的长度相同的数组,但是空(["", ""]);

如果我用JSON.stringify发送,结果是:

{'[{"itemNumber":"13544","currentShelf":"1A1","amount":"1","newShelf":"","actionType":"in","whareHouse":"Main"},{"itemNumber":"13544","currentShelf":"1B1","amount":"1","newShelf":"","actionType":"in", "whareHouse":"Main"}]': '' }

这就是我发送它的方式:

      for (var i=1; i<=m; i++){

            itemIdTemp= document.getElementById("itemIdShell"+i).value;
            shellTemp= document.getElementById("id_shell"+i).value.toUpperCase();
            newShellTemp= document.getElementById("id_shell_new"+i).value.toUpperCase();
            shellAmountTemp = document.getElementById("amountShell"+i).value;
            itemAmount=0;
            let itemData={
                        itemNumber:itemIdTemp,
                        currentShelf:shellTemp,
                        amount:shellAmountTemp,
                        newShell:newShellTemp,
                        actionType:direction,
                        whareHouse:"Main",
                        };
            console.log(itemData);
            itemsObject.push(itemData);

        }
        console.log(itemsObject);



        $.post('/itemShell/updateMulti', 
                    JSON.stringify(itemsObject),
                    function(data){  
                        console.log(data);
                    });

该对象包含一个数组的字符串,我无法得到它。 我试过了Json.Parse(),在这种情况下无效。

有什么建议吗?

3 个答案:

答案 0 :(得分:0)

看看这个示例代码

const jsObjectArray = [
  {name: "Homer", age:56 },
  {name: "Marge", age:50 },
];
const buf = JSON.stringify(jsObjectArray);
console.log("Stringified object: "+buf);
//
// Now convert it back to an object
//
const newObject = JSON.parse(buf);
console.log("Reconstituted object: "+newObject);

它也在这个代码中:

https://codepen.io/mikkel/pen/KRayye

答案 1 :(得分:0)

我发现了问题。 发布到Node时必须声明为JSON类型,因此需要使用ajax:

                $.ajax({
                        url: '/itemShell/updateMulti',
                        type:"POST",
                        data:JSON.stringify(dataTosend),
                        contentType:"application/json; charset=utf-8",
                        dataType:"json",
                        success: function(){}
                   }

我还将其更改为对象类型:

            dataToSend={dataArr:itemsObject}

因此在节点中它显示为数组

答案 2 :(得分:-1)

My Guy在发送之前对字符串做了一点工作

首先获取stringify返回的字符串

var json_string = JSON.stringify(itemsObject);

var string = json_string.replace("'", "\'");

string = '{\'[{"itemNumber":"13544","currentShelf":"1A1","amount":"1",
"newShelf":"","actionType":"in","whareHouse":"Main"},
{"itemNumber":"13544","currentShelf":"1B1","amount":"1",
"newShelf":"","actionType":"in", "whareHouse":"Main"}]\': \'\' }';

first_str = string.split("': ");  // remove the last useless chars

second  = first_str[0].substring(2, first_str[0].length);  // remove the first two chars

$.post('/itemShell/updateMulti', second,
                function(data){  
                    console.log(data);
                });

第二个应该有正确的字符串。

古德纳克