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

share|improve this question

2 Answers 2

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/

share|improve this answer
    
Thank You! Any chance this could be demonstrated with a fiddle on jsfiddle.net for a working example extracting it from the json url? –  Craig Jan 24 at 16:15

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.

share|improve this answer
    
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? –  Craig Jan 24 at 16:12
    
simply place the above code in a callback to a $.getJSON call. don't forget to add &callback=?? to the url. –  Kevin B Jan 24 at 16:14
    
@KevinB please take a look at this jsfiddle.net/XaAP4 ? –  Craig Jan 24 at 16:27
    
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!! –  Kevin B Jan 24 at 16:27
1  
jsfiddle.net/XaAP4/1 –  Kevin B Jan 24 at 16:36

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.