使用 AJAX 解析有效的 JSON?

发布于 2024-11-29 04:53:50 字数 746 浏览 0 评论 0原文

嗨,我仍在学习这个 JSON 的东西...

我想我找到了一个有效的 JSON:

http: //www.nfl.com/liveupdate/scorestrip/ss.json

问题是我不确定如何使用 AJAX 解析它?

这可以吗?我也听说过MooTools 有什么区别?

另外,这里还有一些我只是想玩一下但似乎不起作用的代码:

$.ajax({
    type: "GET",
    url: "http://www.nfl.com/liveupdate/scorestrip/ss.json",
    dataType: "json",
    success: function(data) {
        // Interpret response
       for (var i = 0; i < data.gms.length; i++) {
        document.write("Day: " + data.gms[i][0]);
        document.write("<br/>");
        document.write("Time: " + data.gms[i][1]);
        document.write("<br/><br/>");
    }
    }
});

Hi I am still learning this JSON stuff...

I think I found a valid JSON:

http://www.nfl.com/liveupdate/scorestrip/ss.json

Problem is I am not sure how I can parse this using AJAX?

Is this possible to do? I have also heard of MooTools what is the difference?

Also here is some code I have just to play around but doesn't seem to work:

$.ajax({
    type: "GET",
    url: "http://www.nfl.com/liveupdate/scorestrip/ss.json",
    dataType: "json",
    success: function(data) {
        // Interpret response
       for (var i = 0; i < data.gms.length; i++) {
        document.write("Day: " + data.gms[i][0]);
        document.write("<br/>");
        document.write("Time: " + data.gms[i][1]);
        document.write("<br/><br/>");
    }
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍景流年 2024-12-06 04:53:50

JSON 代表“JavaScript 对象表示法”。 AJAX 代表“异步 Javascript 和 XML”。 MooTools 是一个跨浏览器 JavaScript 框架,它为开发人员提供了面向对象的 JavaScript 工作流程。

不要将苹果和豌豆混淆。这三种技术经常一起使用,但又是不同的东西。 JSON 是一种描述 javascript 对象、数组和文字的表示法; AJAX 用于请求和检索文档(包括 JSON 格式的文档),MooTools 可用于进行 AJAX 调用(可能检索 JSON 格式的文档 - 或 XML 片段)。

JSON 是纯 JavaScript,不需要以任何方式“解析”它,JavaScript 解释器会为你做这件事。 AJAX 通常用于描述 XMLHttpRequest:源自 javascript 的 http 请求,通常检索 JSON 或 XML/HTML 数据。 MooTools 允许您使用相对较少的代码通过其 Request

var request = new Request.JSON({
  url: 'http://www.nfl.com/liveupdate/scorestrip/ss.json',

  onRequest: function(){
    alert('We are loading now ...'); // abuse alert(), quite annoying and so 1990
  },

  onComplete: function(jsonObj) {
    // jsonObj is the retrieved JSON. Access it like you would normal javascript objects and arrays
  }

}).send();

JSON stands for "JavaScript Object Notation". AJAX stands for "Asynchronous Javascript And XML". MooTools is a crossbrowser javascript framework which provides developers an object oriented workflow with javascript.

Don't confuse apples with peas. All three technologies are often used together, but are different things. JSON is a notation to describe javascript objects, arrays and literals; AJAX is for requesting and retrieving documents (including documents in JSON format) and MooTools can be used to make AJAX calls (probably retrieving a document in JSON format – or an XML fragment).

JSON is pure javascript, there is no need to "parse" it in any way, the javascript interpreter will do that for you. AJAX is often used to describe XMLHttpRequests: http requests originating from javascript, often retrieving JSON or XML/HTML data. MooTools allows you with relatively little code to make use of the XMLHttpRequest feature trough its Request class:

var request = new Request.JSON({
  url: 'http://www.nfl.com/liveupdate/scorestrip/ss.json',

  onRequest: function(){
    alert('We are loading now ...'); // abuse alert(), quite annoying and so 1990
  },

  onComplete: function(jsonObj) {
    // jsonObj is the retrieved JSON. Access it like you would normal javascript objects and arrays
  }

}).send();
余生一个溪 2024-12-06 04:53:50

我猜你的问题是因为你发出了跨域请求,而你的浏览器阻止了该请求。要处理来自另一个域的 json 响应,您需要使用 jsonp 数据类型。它基本上是在 ajax 请求中更改 dataType:"jsonp" 。

在同源策略下,server1.example.com 提供的网页无法正常连接或与除 server1.example.com 以外的服务器通信。 HTML 元素是一个例外。利用元素的开放策略,某些页面使用它们来检索对来自其他来源的动态生成的 JSON 格式数据进行操作的 Javascript 代码。这种使用模式称为 JSONP。

请阅读相关内容。有关 jquery ajax 中的 abt json 的更多信息: http://api.jquery.com/jQuery.ajax/< /a>

你更新的代码:

$.ajax({
    type: "GET",
    url: "http://www.nfl.com/liveupdate/scorestrip/ss.json",
    dataType: "jsonp",
    success: function(data) {
        // Interpret response
       for (var i = 0; i < data.gms.length; i++) {
        document.write("Day: " + data.gms[i][0]);
        document.write("<br/>");
        document.write("Time: " + data.gms[i][1]);
        document.write("<br/><br/>");
    }
    }
});

由于该网站似乎不在你的控制之下,你可以让脚本点击你的网址,即你的本地网站网址,其中你的本地服务器现在联系实际的网址获取json并按原样发送它

I am guessing your problem is cos your making a cross domain request and your browser is blocking that requst. To process a json response from another domain you need to use jsonp datatype. Its basically chaging the dataType:"jsonp" in your ajax request.

Under the same origin policy, a web page served from server1.example.com cannot normally connect to or communicate with a server other than server1.example.com. An exception is the HTML element. Taking advantage of the open policy for elements, some pages use them to retrieve Javascript code that operates on dynamically-generated JSON-formatted data from other origins. This usage pattern is known as JSONP.

Do read about it. For more info abt json in jquery ajax: http://api.jquery.com/jQuery.ajax/

your updated code:

$.ajax({
    type: "GET",
    url: "http://www.nfl.com/liveupdate/scorestrip/ss.json",
    dataType: "jsonp",
    success: function(data) {
        // Interpret response
       for (var i = 0; i < data.gms.length; i++) {
        document.write("Day: " + data.gms[i][0]);
        document.write("<br/>");
        document.write("Time: " + data.gms[i][1]);
        document.write("<br/><br/>");
    }
    }
});

since the site doesnt seem to be under your control you can have the script hit your url i.e your local site url wherein your local server now contacts the actual url gets the json and sends it as it is

清眉祭 2024-12-06 04:53:50

我建议您首先研究http请求,当您请求数据或接收数据时发生了什么。
一旦您学习了基础知识,那么请求和接收数据就会很容易。

HTTP 请求

I would recommend you first study the http requrest, Whats going on when you request data or receive data.
Once you learn the basic, then this will be easy to request and receive data.

HTTP Request

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文