javascript - JSON 数据类型 - 数字到达服务器时会转换为字符串

标签 javascript json node.js

我正在加载CSV文件,将其解析为 JSON对象,然后我将这些字符串转换为数字。它们在浏览器控制台中显示为数字,然后使用 AJAX 将数据发送到服务器。当我 console.log 数据时,一切都是字符串?我虽然JSON可以接受多种数据类型,那么为什么它会作为字符串呢?

这是代码。

    for(var i = 0; i < input.files.length; i++){
            var files = input.files[i];

                Papa.parse(files, {
                    header:false,
                    dynamictyping:true,
                    complete: function(results){
                        var input = results.data;

                        if(input[0][0] === 'Symbol' || input[0][0] === 'symbol'){
                            input.shift();
                        }
                        input.forEach(function(input){
                            jsonData.theData = theData;

                            var singleEntry = {
                                "symbol"    : input[0],
                                "date"      : input[1],
                                "open"      : Number(input[2]),
                                "high"      : Number(input[3]),
                                "low"       : Number(input[4]),
                                "close"     : Number(input[5]),
                                "volume"    : Number(input[6])
                                };


                            // Here we will try to do the daily computations of what is needed for data 
                            // such as percentage closed in the day and what not. 

                                var open = singleEntry.open;
                                var high = singleEntry.high;
                                var low = singleEntry.low;
                                var close = singleEntry.close;
                                /*                              
                                console.log(open);
                                console.log(high);
                                console.log(low);
                                console.log(close); */

                                //Get the Math variables for close percentage
                                var spread = high - low;
                                var closeDiff = close - low;
                                var answer = closeDiff / spread;

                                console.log(answer);
                                //Adding day closes to object
                                if (singleEntry.volume === 0){
                                    singleEntry["supportDay"] = false;

                                } else {

                                    if(answer <= .3999){

                                            singleEntry["percentClose"] = answer;
                                            singleEntry["supportDay"] = false;
                                            console.log("answer <= .39999");

                                    } else if (answer > .95) {

                                            singleEntry["percentClose"] = answer;
                                            singleEntry["supportDay"] = true;
                                            singleEntry["peakClose"] = true;
                                            console.log("answer > .95");

                                    } else {

                                            singleEntry["percentClose"] = answer;
                                            singleEntry["supportDay"] = true;                                   

                                    }
                                }

                                jsonData.theData.push(singleEntry);
                                console.log(singleEntry.supportDay);

                            return jsonData;
                            }); // End forEach loop

                        document.getElementById("editor").innerHTML = JSON.stringify(jsonData.theData[0]);

                        } // End Callback Complete      
                }); // End PapaParse
         } // End for loop
 });

正如您所看到的,我可以使用这些对象,这是浏览器上的 console.log 输出,所有代码都在浏览器中。

enter image description here

现在这是服务器的 console.log:

enter image description here

该数据直接导入到数据库中,无需在 Node.js 中使用数据。在我的数据库中,它也作为字符串导入。

想法?我缺少什么?

最佳答案

使用 JSON,您的输入可以是各种数据类型。但是,它会被序列化为一个大字符串,并按原样通过线路发送到您的服务器。在服务器端,需要再次将字符串解析为对象。该步骤将根据您的服务器运行的语言类型而有所不同。

关于javascript - JSON 数据类型 - 数字到达服务器时会转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40512972/

相关文章:

javascript - 在网格中显示组合框的值时出现问题

javascript - 如何使用 Angularjs 1.6 在同一页面中显示动态内容?

java - 从 json 反序列化 org.springframework.data.util.pair

node.js - MongoDB updateOne 为 upsertedId 返回 null

node.js - 为 node.js 构建 libxmljs 时出错

javascript - 从 Javascript 中的字符串模式获取单词

javascript - 如何使用 Lodash 获取 JSON 对象中嵌套字段的索引?

javascript - Tween.js 缓动不起作用

javascript - D3中动态更新图表数据

java - GSON反序列化/序列化具有未确定的键名称的json字符串