1

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?

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. Commented Aug 21, 2013 at 15:04
  • The server linked in the question sends the Access-Control-Allow-Origin:* header, so CORS works fine. Commented Aug 21, 2013 at 15:12

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

You really shouldn't continue supporting ActiveX - it's an out of date technology that's already disabled in IE 11 by default.
@carefulnow Using ActiveX for AJAX was required for IE 6, 7, and 8. When I wrote this answer 4 years ago, those latter two browsers were a significant market share of browsers. To this day, jQuery1.12 includes this backward compatibility consideration even though a majority of MS browser users are on IE 10 or Edge. Considering this "support" for ActiveX costs all of 3 lines of code, and the difference between doing it and not doing it might mean choosing to exclude certain users of your code, I disagree with your comment. We support ActiveX not because we like it, but because we must.
1

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/

4 Comments

The "easiest way" is to add an entire library to your project? Disagree. The code you posted works, don't get me wrong, but "the easiest" is relative at best, and the OP did not specify jQuery.
I don't understand how to access the attributes after you stringify, could you help explain this?
stringify was just a demo. The object is already ready for use: data.wvw_matches.length would give you the length of the array, for example.
@user2593573 if you're having trouble dealing with the json objects in your results, I suggest you start another question detailing your specific problem :)
0

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

1 Comment

A note: OP did not specify jQuery, so this answer may not even work (what if he is using backbone? mootools? nothing?). You don't explain that this code would only work if jQuery is included; don't assume jQuery for everything tagged javascript