user2593573
user2593573

Reputation:

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

Upvotes: 1

Views: 7978

Answers (3)

Chris Baker
Chris Baker

Reputation: 50612

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

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

Upvotes: 2

mynetx
mynetx

Reputation: 898

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

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

Upvotes: 1

Mateusz Nowak
Mateusz Nowak

Reputation: 4121

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

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

Upvotes: 0

Related Questions