为什么ParseFloat在我的json对象上没有正常工作

时间:2014-12-07 13:35:39

标签: javascript string parsefloat

有人可以帮助我解释为什么写入相同的数据没有正确转换为我需要的符号。

我似乎无法找出为什么我的对象无法正确转换为正确转换的示例。

var data2_JSON = document.getElementById('JSON2').value;
var obj2 = JSON.parse(data2_JSON); 



function getMaxY() {
    var max = 0;
    
    //!! only every other item in array is having sumps2
    for(var i = 1; i < obj2.dataset2.length; i+=2) {
        if(Number(obj2.dataset2[i].p1) > max) {
            max = Number(obj2.dataset2[i].p1);
        }
    }
    return Number(max|0);  // convert to integer number
}

var sNum = "1.23e-2";
alert(parseFloat(sNum));

alert(parseFloat(obj2.dataset2[4].p1));
		<textarea id="JSON2"> 

{
        "dataset2": [ {"p1" : "2.097E-15      "} ,{"sumps2" :" 12"},  
{"p1" : "3.6039E-13     "} ,{"sumps2" :"  5"},  
{"p1" : "5.6446E-16     "} ,{"sumps2" :" 14"},  
{"p1" : "-4.9850E-20    "} ,{"sumps2" :"  7"},  

{"p1" : "-3.430E-21     "} ,{"sumps2" :"  6"},  
{"p1" : "9.8509E-20     "} ,{"sumps2" :"  7"},  
{"p1" : "6.4500E-20     "} ,{"sumps2" :"  4"},  
{"p1" : "-4.2150E-20    "} ,{"sumps2" :" 17"},  
{"p1" : "-1.835E-21     "} ,{"sumps2" :" 11"},  

{"p1" : "-4.5872E-20    "} ,{"sumps2" :" 13"},  
{"p1" : "-9.7921E-21    "} ,{"sumps2" :"  9"},  
{"p1" : "-1.2206E-19    "} ,{"sumps2" :"  4"},  
{"p1" : "-3.843E-20     "} ,{"sumps2" :"  3"},  
{"p1" : "-2.1871E-20    "} ,{"sumps2" :" 10"},  

{"p1" : "3.0353E-20     "} ,{"sumps2" :"  7"},                
{"p1" : "3.4013E-20     "} ,{"sumps2" :" 17"},                
{"p1" : "-1.20536E-19   "} ,{"sumps2" :" 17"},  

{"p1" : "2.9374E-19     "} ,{"sumps2" :" 12"},  
{"p1" : "9.5229E-20     "} ,{"sumps2" :" 16"},                                            
                            
{"p1" : "0.15006E-15    "} ,{"sumps2" :"  6"},  
{"p1" : "1.01736E-20    "} ,{"sumps2" :" 10"},                
              
{"p1" : "4.966E-21      "} ,{"sumps2" :"  1"},  
{"p1" : "4.08210E-16    "} ,{"sumps2" :"  7"},                
{"p1" : "2.878973E-15   "} ,{"sumps2" :" 12"},  

{"p1" : "2.49E-15       "} ,{"sumps2" :"  3"},  
{"p1" : "4.4632E-15     "} ,{"sumps2" :"  9"},  
{"p1" : "2.35454E-15    "} ,{"sumps2" :"  6"},  
{"p1" : "7E-18          "} ,{"sumps2" :"  2"},               
              
{"p1" : "9.53764E-15    "} ,{"sumps2" :"  4"},  
{"p1" : "3.32849E-15    "} ,{"sumps2" :"  7"},  
{"p1" : "1.0E-16        "} ,{"sumps2" :"  8"},                
              
{"p1" : "1.88E-11       "} ,{"sumps2" :"  8"},  
{"p1" : "5.16E-21       "} ,{"sumps2" :"  3"},                
{"p1" : "5.95492E-15    "} ,{"sumps2" :"  9"},  

{"p1" : "4.2792E-16     "} ,{"sumps2" :"  5"},  
{"p1" : "7.704E-17      "} ,{"sumps2" :" 12"},  
{"p1" : "1.3051E-14     "} ,{"sumps2" :"  3"},  
{"p1" : "4.809E-17      "} ,{"sumps2" :"  4"},  
{"p1" : "7.092E-15      "} ,{"sumps2" :"  4"},  
              
{"p1" : "4.8590E-15     "} ,{"sumps2" :" 15"},  
{"p1" : "5.849955E-15   "} ,{"sumps2" :" 17"},                
{"p1" : "1.7614E-15     "} ,{"sumps2" :"  7"},  

{"p1" : "1.200E-16      "} ,{"sumps2" :"  2"},  
{"p1" : "7.83718E-17    "} ,{"sumps2" :" 10"},                
{"p1" : "0.01223E-15    "} ,{"sumps2" :"  3"},  
{"p1" : "1.071167E-14   "} ,{"sumps2" :" 12"},  

{"p1" : "3.91070E-16    "} ,{"sumps2" :" 14"},                
{"p1" : "1.99E-12       "} ,{"sumps2" :"  5"},  
{"p1" : "2.56763E-16    "} ,{"sumps2" :"  8"},  
{"p1" : "4.4259E-16     "} ,{"sumps2" :"  5"},  

{"p1" : "1.700E-15      "} ,{"sumps2" :"  4"},  
{"p1" : "1.299202E-15   "} ,{"sumps2" :"  6"},  
{"p1" : "1.516E-16      "} ,{"sumps2" :" 10"},  
{"p1" : "1.889254E-13   "} ,{"sumps2" :"  5"},                

{"p1" : "1.93754256E-13 "} ,{"sumps2" :"  4"},  
{"p1" : "1.1994E-15     "} ,{"sumps2" :"  5"}, 
{"p1" : "2.9E-16        "} ,{"sumps2" :"  2"},  
{"p1" : "6.6E-15        "} ,{"sumps2" :"  3"},  
{"p1" : "2.99E-19       "} ,{"sumps2" :" 10"},  

{"p1" : "6.6212E-16     "} ,{"sumps2" :"  3"},  
{"p1" : "7.7389E-20     "} ,{"sumps2" :" 17"},                
{"p1" : "3.10998E-15    "} ,{"sumps2" :"  7"},                
                                          
{"p1" : "5.509E-14      "} ,{"sumps2" :"  5"},  
{"p1" : "3.06E-17       "} ,{"sumps2" :"  3"}, 

{"p1" : "1.295235E-15   "} ,{"sumps2" :" 20"},                
{"p1" : "6.3562E-16     "} ,{"sumps2" :"  7"},  
{"p1" : "0.381E-15      "} ,{"sumps2" :"  3"},                

{"p1" : "0.21525E-15    "} ,{"sumps2" :"  6"},  
{"p1" : "2.048265E-15   "} ,{"sumps2" :"  8"},  
{"p1" : "7.3505E-18     "} ,{"sumps2" :" 10"},  
{"p1" : "1.7666E-20     "} ,{"sumps2" :"  9"},                
                            
{"p1" : "0.057E-15      "} ,{"sumps2" :"  9"},  
{"p1" : "1.342243E-14   "} ,{"sumps2" :" 15"},  
{"p1" : "2.4075E-19     "} ,{"sumps2" :"  5"},  

{"p1" : "1.3037E-14     "} ,{"sumps2" :" 20"},  
{"p1" : "4.76594E-16    "} ,{"sumps2" :"  3"},                
{"p1" : "4.396862E-15   "} ,{"sumps2" :" 13"},                

{"p1" : "1.5382E-15     "} ,{"sumps2" :" 12"}, 
{"p1" : "5.57316E-15    "} ,{"sumps2" :"  3"},  
{"p1" : "7.9E-20        "} ,{"sumps2" :"  3"},                              
                            
{"p1" : "7.6601E-17     "} ,{"sumps2" :"  3"},                              

{"p1" : "0E-15          "} ,{"sumps2" :"  6"},                
{"p1" : "2.759E-14      "} ,{"sumps2" :" 10"},  
{"p1" : "1.16338E-15    "} ,{"sumps2" :" 15"},  
{"p1" : "39.3444E-15    "} ,{"sumps2" :"  2"},  
              
{"p1" : "5.729370E-20   "} ,{"sumps2" :"  9"},  
{"p1" : "1.48347E-16    "} ,{"sumps2" :" 19"},  
{"p1" : "3.371E-15      "} ,{"sumps2" :"  4"},  
{"p1" : "1.02649E-16    "} ,{"sumps2" :"  6"},  

{"p1" : "0E-15          "} ,{"sumps2" :"  2"},  
{"p1" : "5.753107E-15   "} ,{"sumps2" :" 16"},                
{"p1" : "2.37227E-15    "} ,{"sumps2" :"  7"},  
{"p1" : "1.02119E-14    "} ,{"sumps2" :" 15"},  
              
{"p1" : "36.70E-15      "} ,{"sumps2" :"  3"},                
{"p1" : "0.21E-15       "} ,{"sumps2" :"  3"},                

{"p1" : "5.3E-16        "} ,{"sumps2" :" 15"},                
{"p1" : "1.39825E-15    "} ,{"sumps2" :"  6"},  
{"p1" : "5.82E-12       "} ,{"sumps2" :"  3"},  
{"p1" : "5.582339E-15   "} ,{"sumps2" :" 17"},  

{"p1" : "2.3090E-14     "} ,{"sumps2" :"  6"},                              
{"p1" : "0.206E-15      "} ,{"sumps2" :"  4"},  
{"p1" : "1.17E-21       "} ,{"sumps2" :" 13"}, 
                                          
{"p1" : "0.6968E-15     "} ,{"sumps2" :"  5"},  
{"p1" : "3.0092E-17     "} ,{"sumps2" :"  9"},  
              
{"p1" : "17.727E-15     "} ,{"sumps2" :" 14"},  
{"p1" : "7.36052E-17    "} ,{"sumps2" :"  6"},  
{"p1" : "6.5E-11        "} ,{"sumps2" :"  5"},  
{"p1" : "4.00530E-14    "} ,{"sumps2" :" 15"},  

{"p1" : "1.5509E-14     "} ,{"sumps2" :"  6"},  
{"p1" : "5.354E-15      "} ,{"sumps2" :" 13"},                
{"p1" : "1.60E-16       "} ,{"sumps2" :"  1"},               

{"p1" : "4.22765E-13    "} ,{"sumps2" :"  4"}, 
{"p1" : "425.05E-15     "} ,{"sumps2" :"  6"},                
{"p1" : "11.5E-15       "} ,{"sumps2" :"  3"},  
{"p1" : "5.65E-16       "} ,{"sumps2" :"  3"}, 
              
{"p1" : "5.1784338E-14  "} ,{"sumps2" :" 11"},  
{"p1" : "3.6694515E-15  "} ,{"sumps2" :" 13"},  
{"p1" : "0.44811E-15    "} ,{"sumps2" :"  2"},  
{"p1" : "4.78924621E-13 "} ,{"sumps2" :"  3"}, 

{"p1" : "8.2E-16        "} ,{"sumps2" :"  8"},                
{"p1" : "1.5422691E-14  "} ,{"sumps2" :" 19"},  
{"p1" : "3.94E-15       "} ,{"sumps2" :"  3"},  
{"p1" : "7.65E-15       "} ,{"sumps2" :"  3"},  

{"p1" : "1.426028E-13   "} ,{"sumps2" :" 11"},                
{"p1" : "5.96E-15       "} ,{"sumps2" :"  3"},  
{"p1" : "2.78E-15       "} ,{"sumps2" :"  9"}, 
{"p1" : "1.302098E-15   "} ,{"sumps2" :"  5"},  
                            
{"p1" : "0.000235E-15   "} ,{"sumps2" :"  2"},  
{"p1" : "0.00001235E-15 "} ,{"sumps2" :"  6"},                
              
{"p1" : "5.94681E-17    "} ,{"sumps2" :" 18"},  
{"p1" : "9.5913E-21     "} ,{"sumps2" :"  8"},  
{"p1" : "5.94494E-14    "} ,{"sumps2" :" 12"},  
{"p1" : "1.75E-20       "} ,{"sumps2" :"  7"},  
              
{"p1" : "0.00724E-15    "} ,{"sumps2" :"  6"},  
{"p1" : "4.732E-20      "} ,{"sumps2" :"  2"},                
{"p1" : "2.54237E-14    "} ,{"sumps2" :"  5"},  

{"p1" : "0.08837E-15    "} ,{"sumps2" :"  3"},  
{"p1" : "8.30442E-16    "} ,{"sumps2" :" 12"},                
{"p1" : "1.698E-15      "} ,{"sumps2" :"  3"},  
{"p1" : "29.8087E-15    "} ,{"sumps2" :" 18"},  
              
{"p1" : "0.5479E-15     "} ,{"sumps2" :"  2"},  
{"p1" : "1.99573E-15    "} ,{"sumps2" :"  3"},  
{"p1" : "3.7481E-15     "} ,{"sumps2" :" 19"},  
{"p1" : "7.1229E-15     "} ,{"sumps2" :"  3"},  

{"p1" : "1.046741E-13   "} ,{"sumps2" :" 17"},  
{"p1" : "7.9592E-14     "} ,{"sumps2" :"  3"},  
{"p1" : "1.097087E-14   "} ,{"sumps2" :"  5"},  
{"p1" : "3.82E-15       "} ,{"sumps2" :"  8"},                

{"p1" : "3.1722E-15     "} ,{"sumps2" :" 20"},  
{"p1" : "3.38E-21       "} ,{"sumps2" :"  3"},  
{"p1" : "4.923E-21      "} ,{"sumps2" :"  8"},                
{"p1" : "0.7357E-15     "} ,{"sumps2" :"  3"},  

{"p1" : "6.421E-15      "} ,{"sumps2" :"  8"},  
{"p1" : "0.1525E-15     "} ,{"sumps2" :" 12"},  
{"p1" : "3.798787E-15   "} ,{"sumps2" :" 15"},  
{"p1" : "0.0267E-15     "} ,{"sumps2" :" 17"},  
{"p1" : "0.03191E-15    "} ,{"sumps2" :"  9"},  

{"p1" : "9.15201E-15    "} ,{"sumps2" :" 13"},  
{"p1" : "5.500309E-14   "} ,{"sumps2" :"  3"},  
{"p1" : "6.853E-19      "} ,{"sumps2" :" 12"},  
{"p1" : "0.1260E-15     "} ,{"sumps2" :"  2"},  
{"p1" : "4E-16          "} ,{"sumps2" :"  1"},  

{"p1" : "1.49067E-20    "} ,{"sumps2" :"  5"},  
{"p1" : "7.2819E-15     "} ,{"sumps2" :"  7"},  
{"p1" : "6.983E-14      "} ,{"sumps2" :" 22"},  
{"p1" : "4.4E-20        "} ,{"sumps2" :"  4"},  
{"p1" : "9.2640E-17     "} ,{"sumps2" :" 11"},  

{"p1" : "293.11E-15     "} ,{"sumps2" :"  4"},  
{"p1" : "1.132879E-13   "} ,{"sumps2" :"  4"},  
{"p1" : "1.895713E-14   "} ,{"sumps2" :" 19"},  
{"p1" : "5.704E-15      "} ,{"sumps2" :" 10"},  
{"p1" : "1.22e-14       "} ,{"sumps2" :"  0"},  
              
{"p1" : "5.46883E-15    "} ,{"sumps2" :" 11"},  
{"p1" : "1.75993E-18    "} ,{"sumps2" :"  6"},  
{"p1" : "8.92E-16       "} ,{"sumps2" :"  8"},                

{"p1" : "1.6161E-15     "} ,{"sumps2" :"  9"},                              
{"p1" : "1.682112E-14   "} ,{"sumps2" :" 14"},  
{"p1" : "2.73E-15       "} ,{"sumps2" :"  3"}, 

{"p1" : "16.660E-15     "} ,{"sumps2" :"  5"},                
{"p1" : "9.770E-16      "} ,{"sumps2" :"  4"},                
{"p1" : "7.7860E-21     "} ,{"sumps2" :"  5"},  

{"p1" : "1.079865E-15   "} ,{"sumps2" :" 17"},  
{"p1" : "1.618890E-15   "} ,{"sumps2" :" 14"},  
{"p1" : "4.21E-15       "} ,{"sumps2" :"  3"},                
{"p1" : "5.6E-14        "} ,{"sumps2" :" 30"},  

{"p1" : "0.378E-15      "} ,{"sumps2" :"  1"},  
{"p1" : "0.401E-15      "} ,{"sumps2" :"  3"},  
{"p1" : "3.0825E-15     "} ,{"sumps2" :"  4"}, 
{"p1" : "0.0449E-15     "} ,{"sumps2" :" 12"},  
{"p1" : "1.68114E-16    "} ,{"sumps2" :" 14"},  

{"p1" : "0.139E-15      "} ,{"sumps2" :"  6"},  
{"p1" : "0.58E-15       "} ,{"sumps2" :" 24"},  
{"p1" : "0.7534E-15     "} ,{"sumps2" :"  5"},  
{"p1" : "2.105178E-15   "} ,{"sumps2" :"  7"},  
{"p1" : "2.44174E-15    "} ,{"sumps2" :" 11"},  

{"p1" : "12.169E-15     "} ,{"sumps2" :"  4"},  
{"p1" : "1.89E-17       "} ,{"sumps2" :" 13"},  
{"p1" : "3.47811E-15    "} ,{"sumps2" :"  7"},  
{"p1" : "9.3E-18        "} ,{"sumps2" :"  4"}, 
{"p1" : "1.04550E-16    "} ,{"sumps2" :"  6"},  
              
{"p1" : "1.70932E-15    "} ,{"sumps2" :"  9"},  
{"p1" : "9.963E-16      "} ,{"sumps2" :"  5"},  
{"p1" : "1.279E-15      "} ,{"sumps2" :"  6"},  
{"p1" : "4.437E-15      "} ,{"sumps2" :"  8"},  
              
{"p1" : "9.778E-15      "} ,{"sumps2" :"  9"},                
{"p1" : "1.25008E-13    "} ,{"sumps2" :" 16"}, 
{"p1" : "6.799219E-15   "} ,{"sumps2" :"  5"},  
              
{"p1" : "3.5393E-15     "} ,{"sumps2" :" 12"},  
{"p1" : "0.0390E-15     "} ,{"sumps2" :"  3"},  
{"p1" : "8.1E-16        "} ,{"sumps2" :" 32"},  
{"p1" : "1.644E-15      "} ,{"sumps2" :"  1"},  

{"p1" : "9.497E-15      "} ,{"sumps2" :"  6"},               
{"p1" : "1.7238E-16     "} ,{"sumps2" :" 14"},                
{"p1" : "1.60135E-15    "} ,{"sumps2" :"  3"},  

{"p1" : "119.94E-15     "} ,{"sumps2" :"  2"},                
{"p1" : "7.908E-16      "} ,{"sumps2" :"  5"},  
{"p1" : "4.463E-16      "} ,{"sumps2" :"  3"},  
{"p1" : "6.32095E-15    "} ,{"sumps2" :"  3"},  

{"p1" : "7.26269E-15    "} ,{"sumps2" :"  3"},  
{"p1" : "1.362E-14      "} ,{"sumps2" :"  2"},  
{"p1" : "1.677E-15      "} ,{"sumps2" :" 10"}, 
{"p1" : "2.334387E-14   "} ,{"sumps2" :"  5"},  
{"p1" : "0.00004912E-15 "} ,{"sumps2" :" 12"},  

{"p1" : "8.7494E-14     "} ,{"sumps2" :"  8"},  
{"p1" : "1.07E-16       "} ,{"sumps2" :"  9"}, 
{"p1" : "1.887E-15      "} ,{"sumps2" :" 18"}, 
{"p1" : "4.63E-16       "} ,{"sumps2" :"  6"}, 
{"p1" : "1.49E-16       "} ,{"sumps2" :"  3"},  

{"p1" : "2.489796E-14   "} ,{"sumps2" :"  4"},  
{"p1" : "0.5220E-15     "} ,{"sumps2" :"  3"},  
{"p1" : "1.8303E-15     "} ,{"sumps2" :"  2"}, 
{"p1" : "6.695000E-16   "} ,{"sumps2" :" 17"},  
{"p1" : "1.5151E-14     "} ,{"sumps2" :"  2"},  

{"p1" : "3.33E-16       "} ,{"sumps2" :" 17"},               
{"p1" : "3.59E-15       "} ,{"sumps2" :"  5"},  
{"p1" : "0.01E-15       "} ,{"sumps2" :"  2"},  
{"p1" : "3.60812E-15    "} ,{"sumps2" :"  4"},  

{"p1" : "1.37294E-14    "} ,{"sumps2" :" 10"}, 
{"p1" : "9.7569E-14     "} ,{"sumps2" :"  4"},                              
{"p1" : "3.5477E-14     "} ,{"sumps2" :"  5"}, 

{"p1" : "4.878E-15      "} ,{"sumps2" :"  2"},                              
{"p1" : "2.50E-16       "} ,{"sumps2" :" 15"},  
{"p1" : "8.37E-15       "} ,{"sumps2" :"  8"}

                   ]
} 

		</textarea>

3 个答案:

答案 0 :(得分:0)

它是正确的,因为obj2.dataset2 [4]就是这个对象{&#34; p1&#34; :&#34; 5.6446E-16&#34;}

答案 1 :(得分:0)

假设您要做的是获取p1值的最大值并在不使用科学记数法的情况下显示它,这应该适合您。

// Parse data
var data2_JSON = document.getElementById('JSON3').value;
var obj2 = JSON.parse(data2_JSON); 

// Find maximum p1
function getMaxY() {
    return obj2.dataset2.reduce(function(p, c, i){
        // Ignore missing values - Could use (i % 2) here if the order was known to be consistent. 
        return (typeof(c.p1) === 'undefined') ? p : Math.max(p, parseFloat(c.p1, 10));
    }, 0);
}

// var sNum = "1.23e-2";
// alert(parseFloat(sNum));
// alert(parseFloat(obj2.dataset2[4].p1));

// Use toFixed() to ensure the value is not presented with scientific notation
alert(getMaxY().toFixed(20));
<textarea id="JSON3">
{"dataset2": [
    {"p1" : "2.097E-15      "} ,{"sumps2" :" 12"},
    {"p1" : "3.6039E-13     "} ,{"sumps2" :"  5"},
    {"p1" : "5.6446E-16     "} ,{"sumps2" :" 14"},
    {"p1" : "-4.9850E-20    "} ,{"sumps2" :"  7"},
    {"p1" : "4.878E-15      "} ,{"sumps2" :"  2"},
    {"p1" : "2.50E-16       "} ,{"sumps2" :" 15"},

    {"p1" : "8.37E-15       "} ,{"sumps2" :"  8"}
]}
</textarea>

答案 2 :(得分:0)

最后,我找到了一个不同的解决方案,使Transform函数在画布上呈现宽度和高度变量的数据。

http://jsfiddle.net/16t735wr/

var transx = makeTransform([minlogP0, maxlogP0], [mingraphx, maxgraphx]);
var transy = makeTransform([minlogP1, maxlogP1], [maxgraphy, mingraphy]);

// function to create the needed linear transforms
// note from and to are two element arrays (see below):
function makeTransform(from, to){
    // determine a and b in terms of from[0], from[1], to[0] and to[1]
    var x1 = from[0];
    var x2 = from[1];
    var y1 = to[0];
    var y2 = to[1];

    var a = (y1 - y2) / (x1 - x2);
    var b = y1 - a * x1;

    return function(x){return a * x + b;};
}