Javascript从URL解析json

时间:2016-12-13 21:11:02

标签: javascript json parsing

尝试在javascript中解析来自URL的json响应。

以下是响应的内容

Get-ScheduledTask | where state -EQ 'ready' | Get-ScheduledTaskInfo | where taskpath -eq '\' | Export-Csv -NoTypeInformation -Path C:\fso\scheduledTasksResults.csv

我可以使用

转储json输出
   {"data":[{"version":"7.4.0","startDate":"2016-12-   12","totalSessions":"6208723","totalCrashes":"2944","crashRate":"0.047"},{"version":"7.4.0","startDate":"2016-12-11","totalSessions":"4979676","totalCrashes":"2378","crashRate":"0.048"},{"version":"7.4.0","startDate":"2016-12-10","totalSessions":"534913","totalCrashes":"208","crashRate":"0.039"},{"version":"7.4.0","startDate":"2016-12-09","totalSessions":"309564","totalCrashes":"147","crashRate":"0.047"},{"version":"7.4.0","startDate":"2016-12-08","totalSessions":"255597","totalCrashes":"162","crashRate":"0.063"},{"version":"7.4.0","startDate":"2016-12-07","totalSessions":"21379","totalCrashes":"12","crashRate":"0.056"}]}

然后我尝试使用

解析它
var crash = $.post('http://localhost/crash_stats.php', function(data2) {
    $('#show-list').html(data2); //shows json

});

也尝试了

document.getElementById("placeholder").innerHTML=data2.data[0].version

但没有运气。

1 个答案:

答案 0 :(得分:0)

你应该告诉jQuery AJAX函数返回JSON,然后它会自动解析它。

var crash = $.post('http://localhost/crash_stats.php', function(data2) {
    $("#placeholder").text(data2.data[0].version);
}, 'json');

或者您可以自己致电JSON.parse()

var crash = $.post('http://localhost/crash_stats.php', function(data2) {
    var data = JSON.parse(data2);
    $("#placeholder").text(data.data[0].version);
});