如何将JS对象转换为有效的JSON文件并使用AJAX将其检索回来?

时间:2015-06-17 15:06:54

标签: javascript jquery ajax json

我是JS和JSON的新手。 在我的JS文件中 - 我有

var data = {

      "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":"5", "danger_list":"5,10,15,19,23", "warning":"8", "warning_list":"3,7,11,13,14,16,21,22", "success":"12", "success_list":"1,2,4,6,8,9,12,17,18,20,24,25"},

      "B":{"section_num":"2.3", "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":"10", "danger_list":"11,12,13,14,15,16,17,18,19,20", "warning":"10", "warning_list":"1,2,3,4,5,6,7,8,9,10", "success":"5", "success_list":"21,21,23,24,25"},

      "C":{"section_num":"2.4", "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", "danger_list":"", "warning":"10", "warning_list":"1,2,3,4,5,6,7,8,9,10", "success":"15", "success_list":"11,12,13,14,15,16,17,18,19,20,21,22,23,24,25"},

       "D":{"section_num":"2.5", "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":"5", "danger_list":"1,2,3,4,5", "warning":"0", "warning_list":"", "success":"20", "success_list":"6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25"}
    };

我想要

将此数据移动到外部JSON文件 - 因此我可以向它发出AJAX请求。

我如何完成某些事情?

我应该从哪里开始?

有人可以教我如何做到这一点或指导我走向正确的方向吗?

2 个答案:

答案 0 :(得分:1)

只需从代码中删除var data =,然后将文件保存为" data.json"。然后将该文件放在Web服务器上的某个位置,并通过ajax调用它。

UPDATE 如果你使用jQuery:

$.ajax({
  url: 'your/json/file.json',
  type: 'get',
  datatype: 'json',
  success: function (data) {
    alert(data);
  }
});

答案 1 :(得分:1)

JSON文件

这是您在某个文件中添加的内容,例如data.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":"5", "danger_list":"5,10,15,19,23", "warning":"8", "warning_list":"3,7,11,13,14,16,21,22", "success":"12", "success_list":"1,2,4,6,8,9,12,17,18,20,24,25"},

      "B":{"section_num":"2.3", "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":"10", "danger_list":"11,12,13,14,15,16,17,18,19,20", "warning":"10", "warning_list":"1,2,3,4,5,6,7,8,9,10", "success":"5", "success_list":"21,21,23,24,25"},

      "C":{"section_num":"2.4", "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", "danger_list":"", "warning":"10", "warning_list":"1,2,3,4,5,6,7,8,9,10", "success":"15", "success_list":"11,12,13,14,15,16,17,18,19,20,21,22,23,24,25"},

      "D":{"section_num":"2.5", "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":"5", "danger_list":"1,2,3,4,5", "warning":"0", "warning_list":"", "success":"20", "success_list":"6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25"}
    }

jQuery AJAX调用

如果您的data.json与HTML文件位于同一目录中,那么您可以使用此代码获取它:

$.get('data.json')
  .done(function (data) {
    // you can use your data here
  })
  .fail(function () {
    // something went wrong
  });

(你用&#34标记了你的问题; jquery"所以我在这里使用jQuery。)

MIME类型

确保您的服务器使用正确的MIME类型提供文件: application / json (请参阅RFC 4627RFC 7159)。

JSON语法

请记住,JSON数据的语法比JavaScript对象文字更严格。在您的情况下,这恰好是有效的JSON,但不是每个JavaScript对象都是有效的JSON - 例如,您需要始终引用带双引号的键等。 - 有关详细信息,请参阅:json.org

关于路径的说明

$.get('path')中的路径可以是:

  1. 'data.json'
  2. 'path/data.json'
  3. '/path/data.json'
  4. 我们假设您访问HTML网页:

    http://www.example.com/something/page.html

    此页面包含一些发出AJAX请求的JavaScript文件。对于上面的3个例子,它将分别尝试加载:

    1. http://www.example.com/something/data.json(适用于'data.json'
    2. http://www.example.com/something/path/data.json(适用于'path/data.json'
    3. http://www.example.com/path/data.json(适用于'/path/data.json'
    4. 以另一种方式看待它。如果您将JavaScript包含在HTML文件中,例如:

      <script src="some/path/file.js"></script>
      

      并且您的data.json与JavaScript文件位于同一位置,然后您应该拨打AJAX:

      $.get('some/path/data.json')
      

      路径相对于您的网页,与<script>标记中的路径相同。

      协议

      请注意,它不适用于file: protocol。您需要一个真实的协议,如HTTP或HTTPS,即。您需要使用Web服务器提供文件,而不是在本地文件系统上访问它们。

      更多信息

      另请参阅一些相关的jQuery文档: