2016-11-09 71 views
1

我正在加載CSV文件,將其解析爲JSON對象,然後將這些字符串轉換爲數字。它們在瀏覽器控制檯中顯示爲數字,然後使用AJAX將數據發送到服務器。當我console.log數據一切都是字符串?我雖然JSON可以接受各種數據類型,那麼它爲什麼會以字符串形式出現?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

該數據被直接導入數據庫中不存在與在節點上的數據的工作。在我的數據庫中,它也以字符串形式導入。

想法?我錯過了什麼?

+0

JSON始終是一個字符串表示形式 - 必須對其進行解析以創建一個在JavaScript(或其他語言)中使用的對象,並且一旦發生JavaScript(或其他語言)將結果對象視爲與其他對象相同。 – Falk

+0

JSON是一個序列化爲String的對象表示形式。我懷疑你只需要在接收端調用'JSON.parse'來反序列化對象。 –

回答

1

使用JSON,您的輸入可以是各種數據類型。但是,它會被序列化爲一個大字符串,然後將線路發送到您的服務器。在服務器端,需要將字符串再次解析爲對象。這一步將取決於您的服務器運行在哪種語言。