我使用 data_str = JSON.stringify(data); 转换为字符串。 json中的数据是
[{"temperSensorData":"28.489084691371996","temperSensorUnit":"celsius","timestamp":"1493270171424","timestamp2":"1493270171454","timestamp3":"1493270171454","val":"0"},{"temperSensorData":"28.48908469137112","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171522","timestamp3":"1493270171523","val":"1"},{"temperSensorData":"28.489084691371186","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171523","timestamp3":"1493270171524","val":"2"},{"temperSensorData":"28.489084691371595","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171524","timestamp3":"1493270171525","val":"3"},{"temperSensorData":"28.48908469137168","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"4"},{"temperSensorData":"28.489084691371684","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"5"},{"temperSensorData":"28.489084691371357","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171530","timestamp3":"1493270171531","val":"6"},{"temperSensorData":"28.489084691371144","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171531","timestamp3":"1493270171532","val":"7"},{"temperSensorData":"28.48908469137181","temperSensorUnit":"celsius","timestamp":"1493270171431","timestamp2":"1493270171546","timestamp3":"1493270171546","val":"25"}]
现在我想采用 timestamp 值并执行一些数学运算。如何将其转换为Float值? 即使我使用 parseInt(时间戳)和数字(时间戳),它仍然无效并显示结果: NaN 。
答案 0 :(得分:2)
尝试使用parseFloat(" 28.489084691371996")功能。它会对你有所帮助。
parseFloat("28.489084691371996")
如果您需要进一步的帮助,请告诉我。
答案 1 :(得分:1)
parseFloat() - Mozilla
var arr = [{
"temperSensorData": "28.489084691371996", "temperSensorUnit": "celsius", "timestamp": "1493270171424", "timestamp2": "1493270171454", "timestamp3": "1493270171454", "val": "0"
}, {
"temperSensorData": "28.48908469137112", "temperSensorUnit": "celsius", "timestamp": "1493270171426", "timestamp2": "1493270171522", "timestamp3": "1493270171523", "val": "1"
}, {
"temperSensorData": "28.489084691371186", "temperSensorUnit": "celsius", "timestamp": "1493270171426", "timestamp2": "1493270171523", "timestamp3": "1493270171524", "val": "2"
}, {
"temperSensorData": "28.489084691371595", "temperSensorUnit": "celsius", "timestamp": "1493270171426", "timestamp2": "1493270171524", "timestamp3": "1493270171525", "val": "3"
}, {
"temperSensorData": "28.48908469137168", "temperSensorUnit": "celsius", "timestamp": "1493270171428", "timestamp2": "1493270171529", "timestamp3": "1493270171529", "val": "4"
}, {
"temperSensorData": "28.489084691371684", "temperSensorUnit": "celsius", "timestamp": "1493270171428", "timestamp2": "1493270171529", "timestamp3": "1493270171529", "val": "5"
}, {
"temperSensorData": "28.489084691371357", "temperSensorUnit": "celsius", "timestamp": "1493270171429", "timestamp2": "1493270171530", "timestamp3": "1493270171531", "val": "6"
}, {
"temperSensorData": "28.489084691371144", "temperSensorUnit": "celsius", "timestamp": "1493270171429", "timestamp2": "1493270171531", "timestamp3": "1493270171532", "val": "7"
}, {
"temperSensorData": "28.48908469137181", "temperSensorUnit": "celsius", "timestamp": "1493270171431", "timestamp2": "1493270171546", "timestamp3": "1493270171546", "val": "25"
}]
var t = arr.map(x=>{
return parseFloat(x.timestamp)
})
console.log(t)
结果
[ 1493270171424,
1493270171426,
1493270171426,
1493270171426,
1493270171428,
1493270171428,
1493270171429,
1493270171429,
1493270171431 ]
答案 2 :(得分:1)
使用 parseFloat(data.timestamp)
代替parseInt
,而timestamp
是数据json数组的内部对象值。您可以像这样调用data.timestamp
我创建了一些map函数来分隔timestamp
并在数组中附加
var data = [{
"temperSensorData": "28.489084691371996",
"temperSensorUnit": "celsius",
"timestamp": "1493270171424",
"timestamp2": "1493270171454",
"timestamp3": "1493270171454",
"val": "0"
}, {
"temperSensorData": "28.48908469137112",
"temperSensorUnit": "celsius",
"timestamp": "1493270171426",
"timestamp2": "1493270171522",
"timestamp3": "1493270171523",
"val": "1"
}, {
"temperSensorData": "28.489084691371186",
"temperSensorUnit": "celsius",
"timestamp": "1493270171426",
"timestamp2": "1493270171523",
"timestamp3": "1493270171524",
"val": "2"
}, {
"temperSensorData": "28.489084691371595",
"temperSensorUnit": "celsius",
"timestamp": "1493270171426",
"timestamp2": "1493270171524",
"timestamp3": "1493270171525",
"val": "3"
}, {
"temperSensorData": "28.48908469137168",
"temperSensorUnit": "celsius",
"timestamp": "1493270171428",
"timestamp2": "1493270171529",
"timestamp3": "1493270171529",
"val": "4"
}, {
"temperSensorData": "28.489084691371684",
"temperSensorUnit": "celsius",
"timestamp": "1493270171428",
"timestamp2": "1493270171529",
"timestamp3": "1493270171529",
"val": "5"
}, {
"temperSensorData": "28.489084691371357",
"temperSensorUnit": "celsius",
"timestamp": "1493270171429",
"timestamp2": "1493270171530",
"timestamp3": "1493270171531",
"val": "6"
}, {
"temperSensorData": "28.489084691371144",
"temperSensorUnit": "celsius",
"timestamp": "1493270171429",
"timestamp2": "1493270171531",
"timestamp3": "1493270171532",
"val": "7"
}, {
"temperSensorData": "28.48908469137181",
"temperSensorUnit": "celsius",
"timestamp": "1493270171431",
"timestamp2": "1493270171546",
"timestamp3": "1493270171546",
"val": "25"
}]
console.log(data.map(a=> parseFloat(a.timestamp)))
答案 3 :(得分:1)
这是将JSON转换为对象的好方法......
样品:
data_str=JSON.parse('[{"temperSensorData":"28.489084691371996","temperSensorUnit":"celsius","timestamp":"1493270171424","timestamp2":"1493270171454","timestamp3":"1493270171454","val":"0"},{"temperSensorData":"28.48908469137112","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171522","timestamp3":"1493270171523","val":"1"},{"temperSensorData":"28.489084691371186","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171523","timestamp3":"1493270171524","val":"2"},{"temperSensorData":"28.489084691371595","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171524","timestamp3":"1493270171525","val":"3"},{"temperSensorData":"28.48908469137168","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"4"},{"temperSensorData":"28.489084691371684","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"5"},{"temperSensorData":"28.489084691371357","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171530","timestamp3":"1493270171531","val":"6"},{"temperSensorData":"28.489084691371144","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171531","timestamp3":"1493270171532","val":"7"},{"temperSensorData":"28.48908469137181","temperSensorUnit":"celsius","timestamp":"1493270171431","timestamp2":"1493270171546","timestamp3":"1493270171546","val":"25"}]');
console.log (data_str.length);
console.log (data_str[0].temperSensorData);
console.log(parseFloat(data_str[0].temperSensorData));
myTimeStamp = parseInt(data_str[0].timestamp);
console.log(myTimeStamp);
var theDate = new Date(myTimeStamp);
dateString = theDate.toString();
console.log(dateString);
答案 4 :(得分:1)
<强>样本强>
var jsonObj = [{"temperSensorData":"28.489084691371996","temperSensorUnit":"celsius","timestamp":"1493270171424","timestamp2":"1493270171454","timestamp3":"1493270171454","val":"0"},{"temperSensorData":"28.48908469137112","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171522","timestamp3":"1493270171523","val":"1"},{"temperSensorData":"28.489084691371186","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171523","timestamp3":"1493270171524","val":"2"},{"temperSensorData":"28.489084691371595","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171524","timestamp3":"1493270171525","val":"3"},{"temperSensorData":"28.48908469137168","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"4"},{"temperSensorData":"28.489084691371684","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529","val":"5"},{"temperSensorData":"28.489084691371357","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171530","timestamp3":"1493270171531","val":"6"},{"temperSensorData":"28.489084691371144","temperSensorUnit":"celsius","timestamp":"1493270171429","timestamp2":"1493270171531","timestamp3":"1493270171532","val":"7"},{"temperSensorData":"28.48908469137181","temperSensorUnit":"celsius","timestamp":"1493270171431","timestamp2":"1493270171546","timestamp3":"1493270171546","val":"25"}];
console.log(jsonObj.map(item => parseFloat(item.timestamp)));