0

How would i parse the json data below to out put as

Staring: Will Smith, Bridget Moynahan, Bruce GreenWood

{"query":{"\n| starring       = [[Will Smith]]<br />[[Bridget Moynahan]]<br />[[Bruce Greenwood]]<br />[[James Cromwell]]<br />[[Chi McBride]]<br />[[Alan Tudyk]]}}

This was taken from here

{
    "query": {
        "normalized": [
            {
                "from": "I,_Robot_(film)",
                "to": "I, Robot (film)"
            }
        ],
        "pages": {
            "564947": {
                "pageid": 564947,
                "ns": 0,
                "title": "I, Robot (film)",
                "revisions": [
                    {
                        "contentformat": "text/x-wiki",
                        "contentmodel": "wikitext",
                        "*": "{{Other uses|I, Robot (disambiguation)}}\n{{Infobox film\n| name           = I, Robot\n| image          = Movie poster i robot.jpg\n| caption        = Theatrical release poster\n| director       = [[Alex Proyas]]\n| producer       = [[Laurence Mark]]<br />[[John Davis (producer)|John Davis]]<br />Topher Dow<br />Wyck Godfrey\n| screenplay     = [[Jeff Vintar]]<br />[[Akiva Goldsman]]\n| story          = Jeff Vintar\n| based on       = {{Based on|premise suggested by ''[[I, Robot]]''|[[Isaac Asimov]]}}\n| starring       = [[Will Smith]]<br />[[Bridget Moynahan]]<br />[[Bruce Greenwood]]<br />[[James Cromwell]]<br />[[Chi McBride]]<br />[[Alan Tudyk]]\n| music          = [[Marco Beltrami]]\n| cinematography = Simon Duggan\n| editing        = Richard Learoyd<br />Armen Minasian<br />[[William Hoy]]\n| studio         = [[Davis Entertainment]]<br />[[Laurence Mark Productions]]<br />[[Overbrook Entertainment|Overbrook Films]]<br/>[[Rainmaker Digital Effects]] (Provided)\n| distributor    = [[20th Century Fox]]\n| released       = {{Film date|2004|7|15|international|2004|7|16|United States}}\n| runtime        = 115 minutes\n| country        = United States\n| language       = English\n| budget         = $120 million\n| gross          = $347,234,916\n}}\n'''''I, Robot''''' is a 2004 American [[dystopia]]n [[science fiction film|science fiction]] [[action film]] directed by [[Alex Proyas]]. The screenplay was written by [[Jeff Vintar]] and [[Akiva Goldsman]], and is inspired by (\"suggested by\", according to the end credits) [[Isaac Asimov]]'s short-story collection [[I, Robot|of the same name]]. [[Will Smith]] stars in the lead role of the film as Detective Del Spooner. The supporting cast includes [[Bridget Moynahan]], [[Bruce Greenwood]], [[James Cromwell]], [[Chi McBride]], [[Alan Tudyk]], and [[Shia LaBeouf]]. \n\n''I, Robot'' was released in [[North America]] on July 16, 2004, in [[Australia]] on July 22, 2004, in the [[United Kingdom]] on August 6, 2004 and in other countries between July 2004 to October 2004. Produced with a budget of [[United States dollar|USD]] $120 million, the film grossed $144 million domestically and $202 million in foreign markets for a worldwide total of $347 million. The movie received favorable reviews, with critics praising the writing, visual effects, and acting; but other critics were mixed with the focus on the plot. It was nominated for the 2004 [[Academy Award for Best Visual Effects]], but lost to ''[[Spider-Man 2]]''."
                    }
                ]
            }
        }
    }
}

With the url being:

http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=I,Robot(film)&rvsection=0

Your help would be greatly appreciated.

Thank You,

0

2 Answers 2

0

use:

 var Jsonstring = {title: "Movie", actors: [ 'actor1','actor2']};
 var movie =  $.parseJSON(Jsonstring);
 alert(movie.title); //will alert Movie
 alert(movie.actors[0]) // will alert actor1

this function will convert your json string to javascript object.

http://api.jquery.com/jquery.parsejson/

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

1 Comment

Thank You! Any chance this could be demonstrated with a fiddle on jsfiddle.net for a working example extracting it from the json url?
0

You can parse it with RegExp:

var str = obj.query.pages[564947].revisions[0]['*'],
    matches = str.match(/\|\s+(starring)\s+=\s+(.+)\n/),
    result = matches[1] + ': ' + matches[2].replace(/<br\s+\/>/ig, ', ').replace(/[\[\]]/ig, '');

There will be starring: Will Smith, Bridget Moynahan, Bruce Greenwood, James Cromwell, Chi McBride, Alan Tudyk in the result variable.

4 Comments

Thank you for this! If it isn't too much trouble, is there anyway you could put this in a fiddle for me at jsfiddle.net with a working example extracting it from the direct url?
simply place the above code in a callback to a $.getJSON call. don't forget to add &callback=?? to the url.
You realize, your url isn't wrapped in quotes, right? also, you accepted the parameter named data, but then never used it, and instead used an undefined obj variable. Please, debug!!
The url you are using also seems to differ from the one in your question.

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.