使用JavaScript / Jquery解析JSON数据

时间:2014-01-24 15:40:38

标签: javascript jquery json parsing

我如何将下面的json数据解析为

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]]}}

这是从这里取的

{
    "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]]''."
                    }
                ]
            }
        }
    }
}

网址为:

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

非常感谢您的帮助。

谢谢,

2 个答案:

答案 0 :(得分:0)

使用:

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

此函数会将您的json字符串转换为javascript对象。

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

答案 1 :(得分:0)

您可以使用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, '');

结果变量中会有starring: Will Smith, Bridget Moynahan, Bruce Greenwood, James Cromwell, Chi McBride, Alan Tudyk