2015-10-31 113 views
0

這裏是我的代碼:爲什麼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

+0

使用'console.log'代替alert'的'用於調試JS,'alert'自動數組項轉換爲 – aghidini

回答

0

你的代碼stringifyparse JSON是正確的,你看到的行爲是由於alert功能。 alert自動嘗試將參數轉換爲可讀的字符串(不是JSON字符串),在您的情況下它會將flightPositions數組中的每個項目轉換爲字符串。

要調試JS,您應該使用console.log方法(docs)和瀏覽器開發人員的工具(使用F12打開工具)。

var jsonStr = (JSON.stringify(data, ['flightPositions', 'flightId', 'positions', 'lat', 'lon', 'date'], 4)); 
console.log(jsonStr); 

var jsonObj = JSON.parse(jsonStr); 
console.log(jsonObj.flightPositions); 
+0

謝謝字符串。這是否,但仍然給我相同的輸出(從Firefox控制檯上傳一個新的圖像) – pierrebonbon

+0

你是如何將輸出打印到控制檯的?你必須像我的例子一樣打印對象,而不用連接字符串(例如:'console.log('這些是附近的航班:'+ jsonObj.flightPositions')將不起作用,它會將所有內容再次轉換爲字符串)。 – aghidini

+0

相應地更改,無需連接字符串。給我這個:Array [對象,對象,對象,對象,對象] – pierrebonbon

相關問題