karadayi
karadayi

Reputation: 2269

JSON Parsing using jQuery - Deep Multidimensional Arrays

I have read a lot of Questions here but could find any answer for my Problem with Multidimensional JSON Arrays. I want to just to parse the Tag "countdown" but my Code get only up to the Array "departureTime". Any other change has no result to show the "countdown" tag.

My JSON

    {
    "data": {
        "monitors": [
            {
                "locationStop": {
                    "type": "Feature",
                    "geometry": {
                        "type": "Point"
                    }
                },
                "lines": [
                    {
                        "departures": {
                            "departure": [
                                {
                                    "departureTime": {
                                        "timePlanned": "2016-02-13T23:03:00.000+0100",
                                        "timeReal": "2016-02-13T23:03:39.000+0100",
                                        "countdown": 13
                                    }
                                },
                                {
                                    "departureTime": {
                                        "timePlanned": "2016-02-13T23:33:00.000+0100",
                                        "countdown": 43
                                    }
                                },
                                {
                                    "departureTime": {
                                        "timePlanned": "2016-02-13T23:48:00.000+0100",
                                        "countdown": 58
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        ]
    }
}

I am parsing like here

success: function(data) {
    var json = $.parseJSON(data);
    var departure = json.data.monitors[0].lines[0].departures.departure;
    $.each(departure, function(i, item) {
        $.each(item, function(i, type) {
            console.log(type);
            $('#results').append(type);
        });

    });
}

Adding a third $.each is not working in any way, I added to $each(departure.departureTime) and to $.each(item.countdown) but this give me Errors too. I am not a developer :(

Upvotes: 0

Views: 45

Answers (1)

Linus Oleander
Linus Oleander

Reputation: 18127

Try this:

success: function(data) {
    var json = $.parseJSON(data);
    var departure = json.data.monitors[0].lines[0].departures.departure;
    $.each(departure, function(i, item) {
        $('#results').append(item.departureTime.countdown);
    });
}

Upvotes: 1

Related Questions