我如何收到一个JSON文件中使用AJAX和使用JavaScrip

我如何收到一个JSON文件中使用AJAX和使用JavaScrip

本文介绍了我如何收到一个JSON文件中使用AJAX和使用JavaScript解析它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析这个非常漫长而复杂的JSON是Foursquare的给我。这是我的AJAX请求:

I'm trying to parse this very long and complicated JSON that foursquare gives me. This is my AJAX request:

    $.ajax({
      url: 'https://api.foursquare.com/v2/venues/explore',
      dataType: 'json',
      data: 'limit=7&ll='+latitude+','+longitude+'&client_id='+client_id+'&client_secret='+client_secret+'',
      async: false,
      success: getVenues(data)
});

getVenues是一个函数,我正在做通过JSON进行排序,并在页面上显示相关的东西。我遇到的第一个问题是,我不知道怎么跟成功的功能,它应该处理从服务器接收到的数据 - 是存储在一个变量某处这个数据?我目前在做getVenues(数据),但它告诉我,'数据'是没有定义的变量。网上有很多教程,但是,似乎很乐意与只是在做一个函数,这个神秘的数据和他们似乎工作。

getVenues is a function I'm making to sort through the JSON and display relevant stuff on the page. The first problem I'm having is that I don't know how to tell the success function that it should deal with the data received from the server - is this data stored in a variable somewhere? I'm currently doing getVenues(data) but it's telling me that 'data' is not a defined variable. Many online tutorials, however, seem to be happy with just doing a function to this mystical 'data' and theirs seem to work.

接下来,我无法解析JSON本身。这里是JSON我试图解决的一个缩短的版本: http://pastie.org/4382619 。如何选择地点的名称和标识的等,并显示他们在网页上?

Next, I'm having trouble parsing the JSON itself. Here is a shortened version of the JSON I'm trying to deal with: http://pastie.org/4382619. How do I select the venue names and ID's etc and show them on the page?

感谢

推荐答案

您应该做的:

$.ajax({
    // some other code
    success: getVenues
});

您告诉阿贾克斯:使用getVenues功能,而不是使用getVenus(数据)值。至于第二个问题:

You are telling ajax: "use getVenues function", not "use getVenus(data) value". As for second question:

var l = data.response.groups.length;
for (var i = 0; i < l; i++) {
    var group = data.response.groups[i];
    var k = group.items.length;
    for (var j = 0; j < k; j++) {
        var venue = group.items[j].venue;
        // use venue as you wish
    }
}

这篇关于我如何收到一个JSON文件中使用AJAX和使用JavaScript解析它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:07