這裏是我的代碼:爲什麼JSON.parse不能輸出正確的結果?
var jsonStr = (JSON.stringify(data, ['flightPositions', 'flightId', 'positions', 'lat', 'lon', 'date'], 4));
alert(jsonStr);
var jsonObj = JSON.parse(jsonStr);
alert(jsonObj.flightPositions);
},
從JSON.stringify的輸出似乎是正確的。我已經通過JSLint的運行它,並沒有給我的錯誤:
{
"flightPositions": [
{
"flightId": 621650693,
"positions": [
{
"lat": 40.4272,
"lon": -74.3395,
"date": "2015-10-31T13:31:55.000Z"
},
{
"lat": 40.6833,
"lon": -74.1667,
"date": "2015-10-31T13:32:00.000Z"
},
{
"lat": 40.6133,
"lon": -74.0122,
"date": "2015-10-31T13:32:55.000Z"
},
{
"lat": 40.7538,
"lon": -74.0989,
"date": "2015-10-31T13:33:55.000Z"
},
{
"lat": 40.8125,
"lon": -74.1044,
"date": "2015-10-31T13:34:55.000Z"
},
{
"lat": 40.8147,
"lon": -74.1832,
"date": "2015-10-31T13:35:55.000Z"
},
{
"lat": 40.7624,
"lon": -74.2316,
"date": "2015-10-31T13:36:55.000Z"
},
{
"lat": 40.6829,
"lon": -74.2523,
"date": "2015-10-31T13:37:55.000Z"
}
]
},
{
"flightId": 621650363,
"positions": [
{
"lat": 40.7859,
"lon": -74.0978,
"date": "2015-10-31T13:29:21.000Z"
},
{
"lat": 40.7738,
"lon": -74.143,
"date": "2015-10-31T13:30:21.000Z"
},
{
"lat": 40.7169,
"lon": -74.1948,
"date": "2015-10-31T13:31:21.000Z"
},
{
"lat": 40.6602,
"lon": -74.2524,
"date": "2015-10-31T13:32:21.000Z"
},
{
"lat": 40.5888,
"lon": -74.3109,
"date": "2015-10-31T13:33:21.000Z"
},
{
"lat": 40.5004,
"lon": -74.3464,
"date": "2015-10-31T13:34:21.000Z"
},
{
"lat": 40.4213,
"lon": -74.3074,
"date": "2015-10-31T13:35:21.000Z"
},
{
"lat": 40.3152,
"lon": -74.2497,
"date": "2015-10-31T13:36:21.000Z"
},
{
"lat": 40.2224,
"lon": -74.1577,
"date": "2015-10-31T13:37:21.000Z"
},
{
"lat": 40.163,
"lon": -74.0203,
"date": "2015-10-31T13:38:21.000Z"
}
]
},
{
"flightId": 621650734,
"positions": [
{
"lat": 40.6742,
"lon": -74.1728,
"date": "2015-10-31T13:29:15.000Z"
},
{
"lat": 40.6369,
"lon": -74.1976,
"date": "2015-10-31T13:30:15.000Z"
},
{
"lat": 40.7258,
"lon": -74.1342,
"date": "2015-10-31T13:30:22.000Z"
},
{
"lat": 40.8227,
"lon": -74.0561,
"date": "2015-10-31T13:32:15.000Z"
},
{
"lat": 40.7436,
"lon": -74.2853,
"date": "2015-10-31T13:33:15.000Z"
},
{
"lat": 40.6199,
"lon": -74.2754,
"date": "2015-10-31T13:34:15.000Z"
},
{
"lat": 40.5328,
"lon": -74.2924,
"date": "2015-10-31T13:35:15.000Z"
},
{
"lat": 40.4304,
"lon": -74.2721,
"date": "2015-10-31T13:36:15.000Z"
},
{
"lat": 40.3341,
"lon": -74.2384,
"date": "2015-10-31T13:37:15.000Z"
},
{
"lat": 40.2547,
"lon": -74.173,
"date": "2015-10-31T13:38:15.000Z"
}
]
},
{
"flightId": 621633505,
"positions": [
{
"lat": 40.3481,
"lon": -74.7084,
"date": "2015-10-31T13:29:35.000Z"
},
{
"lat": 40.3788,
"lon": -74.6202,
"date": "2015-10-31T13:30:35.000Z"
},
{
"lat": 40.4126,
"lon": -74.5206,
"date": "2015-10-31T13:31:35.000Z"
},
{
"lat": 40.4436,
"lon": -74.4448,
"date": "2015-10-31T13:32:35.000Z"
},
{
"lat": 40.45,
"lon": -74.4333,
"date": "2015-10-31T13:32:35.000Z"
},
{
"lat": 40.481,
"lon": -74.3348,
"date": "2015-10-31T13:33:47.000Z"
},
{
"lat": 40.5225,
"lon": -74.2702,
"date": "2015-10-31T13:34:47.000Z"
},
{
"lat": 40.5691,
"lon": -74.2228,
"date": "2015-10-31T13:35:47.000Z"
},
{
"lat": 40.6234,
"lon": -74.1737,
"date": "2015-10-31T13:36:47.000Z"
},
{
"lat": 40.6804,
"lon": -74.1765,
"date": "2015-10-31T13:37:47.000Z"
}
]
},
{
"flightId": 621650749,
"positions": [
{
"lat": 40.6906,
"lon": -74.2433,
"date": "2015-10-31T13:34:54.000Z"
},
{
"lat": 40.631,
"lon": -74.5142,
"date": "2015-10-31T13:35:54.000Z"
},
{
"lat": 39.6624,
"lon": -74.6148,
"date": "2015-10-31T13:36:54.000Z"
},
{
"lat": 40.6833,
"lon": -74.1667,
"date": "2015-10-31T13:37:00.000Z"
},
{
"lat": 40.7207,
"lon": -74.1416,
"date": "2015-10-31T13:37:54.000Z"
}
]
}
]
}
alert output from JSON.stringify
但是,我得到了JSON.parse如下:
[object Object],[object Object],[object Object],[object Object],[object Object]
可能是什麼錯在這裏?
UPDATE
到CONSOLE.LOG(不串聯字符串)更改警報,這讓我下面的輸出:
Array [ Object, Object, Object, Object, Object ]
使用'console.log'代替alert'的'用於調試JS,'alert'自動數組項轉換爲 – aghidini