使用json_encode编码PHP数据并在js文件中使用它的问题

Okay, I read a bunch of questions on StackOverflow but didn't seem to solve my problem.

I have a php file like this one:

        <?php
            require "config.php";
            try {
                $conn = new PDO("mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME, DB_USERNAME, DB_PASSWORD);
            }
            catch(PDOException $e) {
                die("Database connection could not be established.");
            }
            $conn->exec("SET NAMES UTF8");
            $sql = "SELECT result.year, SUM(result.grade) AS year_total_points
                    FROM (
                        SELECT *
                            FROM students AS s
                            INNER JOIN points AS p
                            ON s.fn = p.student_fn
                    ) result
                    GROUP BY result.year
                    ORDER BY year_total_points DESC";
            $query = $conn->query($sql);
            if($query->rowCount() > 0) {
                $data = array (
                    "years" => [],
                    "years_total_points" => [],
                    );
                while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
                    $data['years'][] = $row['year'];
                    $data['years_total_points'][] = $row['year_total_points'];
                }
                echo json_encode($data);
            }
            else {
                echo mysql_error();
            }

            $conn = null;
        ?>

Long story short I make a query to the database and get a table with column year and column year_total_points. I want to store the data in an associative array so I can pass it to the js file. Because I later in the js file need an array of all years as strings and an array of all points also as strings, I decided to store them in an associative array. But I think that json_encode doesn't encode it as it should because executing the following code:

        function success(data){
            var barChartData = {
                labels : data["years"],
                datasets : [
                    {
                        fillColor : "rgba(151,187,205,0.5)",
                        strokeColor : "rgba(151,187,205,0.8)",
                        highlightFill : "rgba(151,187,205,0.75)",
                        highlightStroke : "rgba(151,187,205,1)",
                        data : data["years_total_points"]
                    }
                ]
            }
            var ctx = document.getElementById("myChart").getContext("2d");
            window.myBar = new Chart(ctx).Bar(barChartData, {
                responsive : true
            });
        }

        function error(){
            alert('Error!');
        }

        window.onload = function(){
            $.ajax({url: "http://localhost/year_comparison.php"}).done(success).error(error);
        }

it says that data["years"] is undefined and I would expect it to be something like ["2011", "2012", "2013"]. Everything with the query is ok because when I added alert(data); I got a result {"years":["2011"],"years_total_points":["85.98"]}. But alert(data["years"]) says undefined. Any help to solve this issue would be greatly appreciated.