0

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/

1
  • i use it to get the information faster Commented Nov 27, 2013 at 3:59

2 Answers 2

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

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

Comments

0

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.

6 Comments

Hello Ruben, is there a way around this possibly? I'm looking for an automated way of doing this with other pages/films
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...
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
i"m not fully informed regarding regex but if there is a constant pattern such as starring ="" wouldn't it work?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.