2014-01-24 2 views
0

Как бы я разобрать данные JSON ниже, чтобы из положить вИспользование JavaScript/Jquery для разбора 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]]''." 
        } 
       ] 
      } 
     } 
    } 
} 

С URL являющиеся:

http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=I,Robot(film)&rvsection=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/

+0

Спасибо! Любой шанс это можно продемонстрировать с помощью скрипта на http://jsfiddle.net/ для рабочего примера, извлекая его из json-url? – Craig

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.

+0

Спасибо за это! Если это не так уж сложно, есть ли в любом случае, вы могли бы поставить это в скрипке для меня на http://jsfiddle.net/ с рабочим примером, извлекая его из прямого URL-адреса? – Craig

+0

просто поместите вышеуказанный код в обратном вызове к вызову $ .getJSON. не забудьте добавить '& callback = ??' в url. –

+0

@KevinB, пожалуйста, взгляните на это http://jsfiddle.net/XaAP4/? – Craig

Смежные вопросы