Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do I parse the content from this url in JSON?

http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=RoboCop_(2014_film)&rvsection=0

I am trying to parse this json url with this code i found below, however it does not work.

$.getJSON(url, function (json) {
    alert(json.result);
    $.each(json.list, function (i, fb) {
        alert(fb.result);
    });
});

Here is what i have on JS Fiddle http://jsfiddle.net/94Vyf/

If I'm not mistaken, this should pop up a window with some sort of information.

Basically i am trying to get the cast of movie via wikipedia's api.

i narrowed down the result, however i just can't seem to find a way to get just the starring value from the data.

Please help this newbie.

Thank You/

share|improve this question
    
i use it to get the information faster –  Craig Nov 27 '13 at 3:59

2 Answers 2

Since it is a cross domain request you need to use jsonp - the callback=? at the end of the url

$.getJSON('http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=RoboCop_(2014_film)&rvsection=0&callback=?', function (json) {
    console.log(json);
    $.each(json.list, function (i, fb) {
        alert(fb.result);
    });
});

Demo: Fiddle - look at the browser console to see the logging statements, don't use alert for debugging purposes

Also in the fiddle you didn't include jQuery

share|improve this answer

DEMO

You not quite right with the URL content, but you can change it as you like.

var url = "http://en.wikipedia.org/w/api.php?action=mobileview&format=json&page=RoboCop_(2014_film)&redirect=no&sections=all&prop=text&sectionprop=level&noimages=&noheadings=&callback=?";


jQuery.getJSON(
    url,
    function (data){ 
      var t = data.mobileview.sections[2].text, out = [];
      t = t.replace(/<a[^>]+>([^<]+)<\/a>/igm,"$1");

      var m = t.match(/(?:<li>)([a-zA-Z ]+) as ([a-zA-Z ]+)(?:,)/img);
      m.forEach(function (s){
        out.push(s.replace(/(?:<li>)([a-zA-Z ]+) as ([a-zA-Z ]+)(?:,)/i, "$1 = $2"));
      });
      console.log(out);// this is result, but you have to modify it as your taste
    }
);

output:

["Joel Kinnaman = Alex Murphy", "Michael Keaton = Raymond Sellars", "Abbie Cornish = Clara Murphy", "Jackie Earle Haley = Maddox", "Michael Kenneth Williams = Officer Jack Lewis", "Jennifer Ehle = Liz Kline", "Jay Baruchel = Tom Pope", "Aimee Garcia = Kim", "John Paul Ruttan = David Murphy", "Douglas Urbanski = Mayor Durant"]

INPORTANT!

This code not will work with another wikipedia pages, because it depends of JSON data and in this case, "the Cast" section have an index 2. Another "film" pages may have different structure, dependent to the author/s.

share|improve this answer
    
Hello Ruben, is there a way around this possibly? I'm looking for an automated way of doing this with other pages/films –  Craig Nov 27 '13 at 4:04
    
Of course not, there is not an universal way. It's a totally human input on a site and as result -- unpredictable composition of pages. But might be imdb.com better struvtured? There are the ID = "titleCast" in the Cast section... –  Ruben Kazumov Nov 27 '13 at 4:15
    
I see what you're saying in regards to to random imput based on the human submission. However there is an constant with the page itself in the non mobile version. The Robocop link for example lines up perfectly with other film submissions, hence why i asked if perhaps i could do it that way.......Also couldn't I possibly parse the json data with regex? In the data for every film I came accross the same line stating "starring = {{Plainlist |\n*[[Joel Kinnaman]]\n*[[Gary Oldman]]\n*[[Michael Keaton]]\n*[[Samuel L. Jackson]]\n}}\n|" actor name being different ofcourse –  Craig Nov 27 '13 at 4:23
    
i"m not fully informed regarding regex but if there is a constant pattern such as starring ="" wouldn't it work? –  Craig Nov 27 '13 at 4:24
    
Dear Craig. You should to give a hug to regex. It's ONLY way to parse everything on a planet Earth in current century. Actually you can recognize any input you can predict. The question just in amount of code you have to write. I answered on you particular question, but it's just one question in an enormous size problem you going to solve. –  Ruben Kazumov Nov 27 '13 at 4:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.