如何基于JSON对象动态创建变量?

时间:2015-06-17 19:51:45

标签: javascript jquery

我有:外部JSON文件

{

    "A": {
        "section_num": "2.2",
        "problem_set": "Same",
        "start_time": "7/20/2015 10:00 am",
        "student_am": "9",
        "due_time": "7/20/2015 11:00 am",
        "submit": "9",
        "avg_score": "71",
        "danger": "0",
        "warning": "2",
        "success": "3"

    },

    "B": {
        "section_num": "2.2",
        "problem_set": "Not the same",
        "start_time": "6/19/2015 1:00 pm",
        "student_am": "23",
        "due_time": "6/19/2015 2:00 pm",
        "submit": "7",
        "avg_score": "82",
        "danger": "1",
        "warning": "2",
        "success": "2"

    },

    "C": {
        "section_num": "2.2",
        "problem_set": "Math",
        "start_time": "6/20/2015 2:00 pm",
        "student_am": "23",
        "due_time": "6/20/2015 3:00 pm",
        "submit": "3",
        "avg_score": "82",
        "danger": "0",
        "warning": "2",
        "success": "3"

    },

    "D": {
        "section_num": "2.2",
        "problem_set": "Anything",
        "start_time": "6/20/2015 7:00 am",
        "student_am": "25",
        "due_time": "6/20/2015 8:00 am",
        "submit": "9",
        "avg_score": "98",
        "danger": "1",
        "warning": "0",
        "success": "4"

    }
}

我使用Ajax

加载它们
$.ajax({

      url: basePath + "data-r.json",
      type: "GET",
      dataType : "json",

      success: function( data ) {

         google.load("visualization", "1", {packages:["corechart"]});
         google.setOnLoadCallback(drawChart());

         function drawChart() {

           var options = {
            width: 160,
            height: 160,
            chartArea: {
              left: 10,
              top: 20,
              width: "100%",
              height: "100%"
            },


            colors: ['#F46E4E',  '#F9C262' , '#ADB55E',],
            legend: 'none',
            enableInteractivity: false,
            pieSliceText: 'none',

          };

          var chart = new google.visualization.PieChart(document.getElementById('piechart'));



          for (var object in data) {

            var total = data[object].danger + data[object].warning + data[object].success ;
            var object = google.visualization.arrayToDataTable([
            ['Piechart', 'Number of Skills'],
            ['danger',  ( data[object].danger/total )  * 100  ],
            ['warning', ( data[object].warning/total ) * 100  ],
            ['success', ( data[object].success/total ) * 100  ],
            ]);

          }

      chart.draw(A, options);

    }

我尝试根据我的JSON中的那些数据创建变量,但我无法

for (var object in data) {

            var total = data[object].danger + data[object].warning + data[object].success ;
            var object = google.visualization.arrayToDataTable([
            ['Piechart', 'Number of Skills'],
            ['danger',  ( data[object].danger/total )  * 100  ],
            ['warning', ( data[object].warning/total ) * 100  ],
            ['success', ( data[object].success/total ) * 100  ],
            ]);

          }

显然我失败了。

当我尝试绘制第一张图表chart.draw(A, options);

我得到了

  

未捕获的ReferenceError:未定义A

0 个答案:

没有答案