javascript - Accessing JSON data from a url - Stack Overflow

I'm fairly new to web programming, so I'm sorry in advance if this is a dumb question. I'

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. 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. 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?

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. 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. 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?

Share Improve this question asked Aug 21, 2013 at 15:00 user2593573user2593573 2
  • You need to use AJAX to download the JSON file. But, JavaScript can't download files from other domains unless the remote domain allows it (CORS or JSONP), so you might need to use a server-side "proxy" to get it. – gen_Eric Commented Aug 21, 2013 at 15:04
  • The server linked in the question sends the Access-Control-Allow-Origin:* header, so CORS works fine. – mynetx Commented Aug 21, 2013 at 15:12
Add a ment  | 

3 Answers 3

Reset to default 2

There are many reasons to add jQuery to a project. BUT. Please don't add jQuery just to get some json data. Javascript is perfectly capable of handling this one on its own, thank you:

// 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./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/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).

And keep in mind: use jQuery when you need it, not for everything and anything.

Documentation

  • XMLHttpRequest on MDN - https://developer.mozilla/en-US/docs/Web/API/XMLHttpRequest
  • XMLHttpRequest ON MSDN (IE) - http://msdn.microsoft./en-us/library/ie/ms535874%28v=vs.85%29.aspx
  • JSON on MDN - https://developer.mozilla/en-US/docs/JSON
  • for... on MDN - https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/for

An easy way of getting the JSON data is by using jQuery, like this:

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

<script src="http://code.jquery./jquery-1.10.2.min.js"></script>
<script>
$(function () {
    $.getJSON(
        "https://api.guildwars2./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/mynetx/LwNKC/

Look at my code below. I used jquery to get content

var result;

$.get(
    "https://api.guildwars2./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 :).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745596169a4635128.html

相关推荐

  • javascript - Accessing JSON data from a url - Stack Overflow

    I'm fairly new to web programming, so I'm sorry in advance if this is a dumb question. I'

    22小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信