如何访问JSON数据?

时间:2015-01-16 19:54:25

标签: javascript jquery json

每次我发出警报时如何访问JSON数据它显示未定义。不知道问题出在哪里。

var FJS = FilterJS(art, '#movies', {

    template: '#movie-template',
    search: {
        ele: '#searchbox'
    },
    callbacks: {
        afterFilter: function(result) {
            $('#total_movies').text(result.length);
            //  alert(art);// this is the alert which is showing array of objects.
            len = result.data.onwardflights[1].origin;
            alert(len);
        }
    }
});

这是JSON树。

{
"data": {
    "returnflights": [], 
    "onwardflights": [
        {
            "origin": "delhi", 
            "status": "V", 
            "DepartureTime": "21:14", 
            "cancellationPolicy": "0:4:100:0;4:12:25:0;12:-1:10:0", 
            "cCount": 0, 
            "farebasis": "0", 
            "seat": "ST", 

2 个答案:

答案 0 :(得分:1)

如果结果是数组

,请尝试此操作
len = result[0].origin;
            alert(len);

答案 1 :(得分:0)

首先,您需要了解结果。

在您需要在json树中找到您的数据并记住 onwardflights 必须数组并拥有您需要的属性。

 callbacks: {
        afterFilter: function(result) {
            $('#total_movies').text(result.length);
            //  alert(art);// this is the alert which is showing array of objects.
            len = result.data.onwardflights[0].origin;
            alert(len);
        }
    }