2012-05-06 12 views
1

jsonで読み込んでオブジェクトの配列を作成しようとしています。これは、関連するコードです:jsonを読み込んでjQueryでオブジェクトの配列を作成します。

//president object consctructor 
function president(a_presName, a_presDates, a_presNick, a_presImage) { 
    this.presName=a_presName; 
    this.presDates=a_presDates; 
    this.presNick=a_presNick; 
    this.presImage=a_presImage; 
} 


var myPres = new Array(); 

$(document).ready(function() { 



    $.getJSON('Presidents.json', function(data) { 


     $.each(data.presidents, function (i, item) { 
      myPres[i]=new president(item.presName, item.presDates, item.presNick, item.PresImage); 
     }); 

    }); //end getJSON 
    document.write(myPres[1].presName); 
}); //end doc ready 

そして、これは、JSONです:

{ "presidents": [ { "presName":"George Washington", "presDates":"1789-1797", "presNick":"Father of His Country", "presImage":"gwashington.gif" }, { "presName":"John Adams", "presDates":"1797-1801", "presNick":"Atlas of Independence", "presImage":"jadams.gif" }, { "presName":"Thomas Jefferson", "presDates":"1801-1809", "presNick":"Sage of Monticello", "presImage":"tjefferson.gif" }, { "presName":"James Madison", "presDates":"1809-1817", "presNick":"Father of the Constitution", "presImage":"jmadison.gif" }, { "presName":"James Monroe", "presDates":"1817-1825", "presNick":"Era-of-Good-Feeling President", "presImage":"jmonroe.gif" }, { "presName":"John Quincy Adams", "presDates":"1825-1829", "presNick":"Old Man Eloquent", "presImage":"jqadams.gif" }, { "presName":"Andrew Jackson", "presDates":"1829-1837", "presNick":"Old Hickory", "presImage":"ajackson.gif" }, { "presName":"Martin Van Buren", "presDates":"1837-1841", "presNick":"The Little Magician", "presImage":"mvanburen.gif" }, { "presName":"William Henry Harrison", "presDates":"1841", "presNick":"Old Tippecanoe", "presImage":"whharrison.gif" }, { "presName":"John Tyler", "presDates":"1841-1845", "presNick":"Accidental President", "presImage":"jtyler.gif" }, { "presName":"James Knox Polk", "presDates":"1845-1849", "presNick":"Young Hickory", "presImage":"jkpolk.gif" }, { "presName":"Zachary Taylor", "presDates":"1849-1850", "presNick":"Old Rough and Ready", "presImage":"ztaylor.gif" }, { "presName":"Millard Fillmore", "presDates":"1850-1853", "presNick":"The American Louis Philippe", "presImage":"mfillmore.gif" }, { "presName":"Franklin Pierce", "presDates":"1853-1857", "presNick":"Young Hickory of the Granite Hills", "presImage":"fpierce.gif" }, { "presName":"James Buchanan", "presDates":"1857-1861", "presNick":"Old Buck", "presImage":"jbuchanan.gif" }, { "presName":"Abraham Lincoln", "presDates":"1861-1865", "presNick":"Honest Abe", "presImage":"alincoln.gif" }, { "presName":"Andrew Johnson", "presDates":"1865-1869", "presNick":"None", "presImage":"ajohnson.gif" }, { "presName":"Ulysses Simpson Grant", "presDates":"1869-1877", "presNick":"Hero of Appomattox", "presImage":"usgrant.gif" }, { "presName":"Rutherford Birchard Hayes", "presDates":"1877-1881", "presNick":"Dark-Horse President", "presImage":"rbhayes.gif" }, { "presName":"James Abram Garfield", "presDates":"1881", "presNick":"None", "presImage":"jagarfield.gif" }, { "presName":"Chester Alan Arthur", "presDates":"1881-1885", "presNick":"The Gentleman Boss", "presImage":"caarthur.gif" }, { "presName":"Grover Cleveland", "presDates":"1885-1889", "presNick":"None", "presImage":"gcleveland.gif" }, { "presName":"Benjamin Harrison", "presDates":"1889-1893", "presNick":"Kid Gloves Harrison", "presImage":"bharrison.gif" }, { "presName":"Grover Cleveland", "presDates":"1893-1897", "presNick":"None", "presImage":"gcleveland.gif" }, { "presName":"William McKinley", "presDates":"1897-1901", "presNick":"Idol of Ohio", "presImage":"wmckinley.gif" }, { "presName":"Theodore Roosevelt", "presDates":"1901-1909", "presNick":"Trust-Buster", "presImage":"troosevelt.gif" }, { "presName":"William Howard Taft", "presDates":"1909-1913", "presNick":"None", "presImage":"whtaft.gif" }, { "presName":"Woodrow Wilson", "presDates":"1913-1921", "presNick":"Schoolmaster in Politics", "presImage":"wwilson.gif" }, { "presName":"Warren Gamaliel Harding", "presDates":"1921-1923", "presNick":"None", "presImage":"wgharding.gif" }, { "presName":"Calvin Coolidge", "presDates":"1923-1929", "presNick":"Silent Cal", "presImage":"ccoolidge.gif" }, { "presName":"Herbert Clark Hoover", "presDates":"1929-1933", "presNick":"None", "presImage":"hchoover.gif" }, { "presName":"Franklin Delano Roosevelt", "presDates":"1933-1945", "presNick":"FDR", "presImage":"fdroosevelt.gif" }, { "presName":"Harry S. Truman", "presDates":"1945-1953", "presNick":"Give 'Em Hell Harry", "presImage":"hstruman.gif" }, { "presName":"Dwight David Eisenhower", "presDates":"1953-1961", "presNick":"Ike", "presImage":"ddeisenhower.gif" }, { "presName":"John Fitzgerald Kennedy", "presDates":"1961-1963", "presNick":"JFK", "presImage":"jfkennedy.gif" }, { "presName":"Lyndon Baines Johnson", "presDates":"1963-1969", "presNick":"LBJ", "presImage":"lbjohnson.gif" }, { "presName":"Richard Milhous Nixon", "presDates":"1969-1974", "presNick":"None", "presImage":"rmnixon.gif" }, { "presName":"Gerald Rudolph Ford", "presDates":"1974-1977", "presNick":"Jerry", "presImage":"grford.gif" }, { "presName":"James Earl Carter Jr.", "presDates":"1977-1981", "presNick":"Jimmy", "presImage":"jecarter.gif" }, { "presName":"Ronald Wilson Reagan", "presDates":"1981-1989", "presNick":"The Gipper", "presImage":"rwreagan.gif" }, { "presName":"George Herbert Walker Bush", "presDates":"1989-1993", "presNick":"Poppy", "presImage":"ghwbush.gif" }, { "presName":"William Jefferson Clinton", "presDates":"1993-2001", "presNick":"Bill", "presImage":"wjclinton.gif" }, { "presName":"George Walker Bush", "presDates":"2001-2009", "presNick":"W", "presImage":"gwbush.gif" }, { "presName":"Barack Hussein Obama", "presDates":"2009-", "presNick":"None", "presImage":"bhobama.gif" } ] } 

私がテストするのdocument.writeを使用する場合、それが置かれていますならば、それは右}); //end getJSON前に正常に動作しますが、それはすぐに場所だ場合それがヌルの後に。私は何が欠けていますか?

+4

を望んでいた何か他のものである場合は、その事実を逃しているを教えてください[AJAX呼び出しはデフォルトで非同期です(http://stackoverflow.com/a/10370489/1233508)。 – DCoder

+1

''新大統領 'では、単に 'data.presidents'配列のオブジェクトの1対1のコピーを作成するだけです。 'president.prototype'で定義されたメソッドがない限り、これは不要です。表示したコードでは、単に 'myPres = data.presidents;'を実行すれば同じ結果が得られます。 –

+0

@FelixKlingは頭をアップしてくれてありがとう。 – MDMarra

答えて

3

//最後のgetJSONは、JSONが取得される前に実行されることを意味します。

.getJSONに渡す匿名関数は、サーバーからデータが戻ったときに呼び出されます。 document.writeを// getJSONの後に実行すると、要求が戻ってくる前に呼び出されます。

+0

+1答えに「成功」​​コールバック関数の説明を追加するとよいでしょう。 – Paulpro

+0

はい、遅い入力=私。 – joshp

+0

リクエストが完了するのを待つためにできることがありますか、または、//そのデータを '// end getJSON'の中で使用するために必要なコードを置くだけですか?私はJS/JQueryの新機能です。 – MDMarra

0

私はjQueryの$ .each() を好きではない私はこのようにそれを行うだろう:

function parseJSON(JSONObj){ 
    var array=new Array(); 
    var count=0; 
    for(var Obj in JSONObj){ 
     array[count]=JSONObj[Obj]; 
     count++; 
    } 
} 

それはあなたが

+0

この場合のように(コメントで指摘されているように)、.each()または他のループは即時です。私自身の知識のために、あなたがそれを気に入らないという事実以外に '$ .each()'を使わない魅力的な理由はありますか? – MDMarra

+1

@MDMarra:このオプションを使用せずに '$ .each()を使うのは正当な理由があります(http://stackoverflow.com/questions/500504/javascript-for-in-with-arrays)代わりに。 – DCoder