简体   繁体   English

从URL访问JSON数据

[英]Accessing JSON data from a url

I'm fairly new to web programming, so I'm sorry in advance if this is a dumb question. 我对网络编程很新,所以如果这是一个愚蠢的问题,我很抱歉。 I've looked around and wasn't able to find anything very concrete on this so I figured I'd ask here. 我环顾四周,无法找到任何非常具体的东西,所以我想我会在这里问。

I'm trying to make a script which reads a JSON file and returns some data. 我正在尝试创建一个读取JSON文件并返回一些数据的脚本。 More specifically here's a link . 更具体地说,这是一个链接

I want to search through and find where an world_id is equal to xxxx , and return the match_id . 我想搜索并找到world_id等于xxxx ,并返回match_id In another thread it one of the solutions was something similar to 在另一个线程中,其中一个解决方案类似于

var obj = JSON.parse(//JSON info here)
var a = obj.world_id

Can anyone point me in the right direction as to achieve this? 任何人都能指出我正确的方向来实现这一目标吗?

There are many reasons to add jQuery to a project. 将jQuery添加到项目有很多原因。 BUT. 但。 Please don't add jQuery just to get some json data . 请不要只是添加jQuery 来获取一些json数据 Javascript is perfectly capable of handling this one on its own, thank you: Javascript完全能够自己处理这个,谢谢:

// simple cross-browser ajax helper
var ajaxGet = function (url, callback) {
    var callback = (typeof callback == 'function' ? callback : false), xhr = null;
    try {
      xhr = new XMLHttpRequest();
    } catch (e) {
      try {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    if (!xhr)
           return null;
    xhr.open("GET", url,true);
    xhr.onreadystatechange=function() {
      if (xhr.readyState==4 && callback) {
        callback(xhr.responseText)
      }
    }
    xhr.send(null);
    return xhr;
}

// example usage, grab the json data, loop it and log red_world_id to console
ajaxGet(
    'https://api.guildwars2.com/v1/wvw/matches.json', 
    function (response) {
        response = JSON.parse(response);
        if (!response)
            return;
        var i, list = response.wvw_matches;
        for (i in list) {
            console.log(list[i].red_world_id); // outputs an id
        }
});

Try it here: http://jsfiddle.net/7WrmL/ 在这里试试: http//jsfiddle.net/7WrmL/

So basically, for your specific usage, instead of simply logging the ID to console, you can check each object's id properties against the desired matching id and, for example, return i for the index of the match (not sure I understand exactly what you're after there). 所以基本上,对于您的特定用法,您可以根据所需的匹配ID检查每个对象的id属性,而不是简单地将ID记录到控制台,例如,返回i作为匹配的索引(不确定我完全理解您的内容)在那之后。

And keep in mind: use jQuery when you need it , not for everything and anything. 请记住: 在需要时使用jQuery ,而不是用于所有内容。

Documentation 文档

An easy way of getting the JSON data is by using jQuery, like this: 获取JSON数据的简单方法是使用jQuery,如下所示:

<div id="reply"></div>

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(function () {
    $.getJSON(
        "https://api.guildwars2.com/v1/wvw/matches.json",
        function (data) {
            $("#reply").html(JSON.stringify(data));
            // or work with the data here, already in object format
        });
});
</script>

See here: http://jsfiddle.net/mynetx/LwNKC/ 见这里: http//jsfiddle.net/mynetx/LwNKC/

Look at my code below. 看下面的代码。 I used jquery to get content 我用jquery来获取内容

var result;

$.get(
    "https://api.guildwars2.com/v1/wvw/matches.json",
    {},
    function(data) {
       var result = data;
    }
);

var arr = JSON.parse(result);

var length = arr.length;

for (var i = 0; i < length; i++) 
{
  if(arr[i].red_world_id == 'xxx')
  {
        console.log('Got it');
  }

  if(arr[i].blue_world_id== 'xxx')
  {
        console.log('Got it');
  }

  if(arr[i].green_world_id== 'xxx')
  {
        console.log('Got it');
  }
}

Look out for slip of the pen :). 留意笔的滑动:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM