为什么JSON.parse没有输出正确的结果?

时间:2015-10-31 13:13:23

标签: json

这是我的代码:

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]

这里可能有什么问题?

更新

将警报更改为console.log(没有连接字符串),它给出了以下输出:

Array [ Object, Object, Object, Object, Object ]

console

1 个答案:

答案 0 :(得分:0)

您的 import X._ List(1,2,3) progress { x => Thread sleep 1000 ; x } Array(1,2,3) progress { x => Thread sleep 1000 ; x } stringify JSON代码是正确的,您看到的行为是由parse函数引起的。 alert会自动尝试将参数转换为人类可读的字符串(而不是JSON字符串),在这种情况下,它会将alert数组中的每个项目转换为字符串。

要调试JS,您应该使用flightPositions方法(docs)和浏览器开发人员的工具(使用F12打开工具)。

console.log