2012-05-09 4 views
1

CrunchBase API의 JSON 오브젝트를 사용하여 주어진 회사에서 많은 정보를 얻을 수 있습니다. 지금은 JSON 객체를 통해 투자자 목록을 작성하려고합니다. 투자자는 세 가지 범주 인 "company", "financial_org", or "person" 중 하나에 속할 수 있습니다. 세 가지 유형 모두 동일한 목록에 추가됩니다.JSON 오브젝트의 내부 깊이 정보 얻기

스크립트는 오류없이 실행되지만 첫 번째 나열된 라운드의 투자자 목록 만 생성합니다. 나는 도움이 될 것이라고 생각하는 모든 것을 기록했다. 로그는 같은 줄 주석으로 표시됩니다.

기본적으로 내 문제는 단지 한 번에 반복되므로 첫 번째 라운드에서 투자자를 추가하는 것입니다. 어떤 도움이라도 대단히 감사하겠습니다. 더 많은 정보가 필요하면 알려주세요. I 필요한 단지 비트으로 단축 한 다음과 같이 데이터에 의해서 표현

var investorList = function(data, num) { 
     var fundingRounds = data["funding_rounds"]; 
     var finalInvestorList = [] 
     console.log(fundingRounds.length) // 3 


     for (i=0; i < fundingRounds.length; i++) { 
      var investments = data["funding_rounds"][i]["investments"]; 
      console.log(data["funding_rounds"][1]["investments"]); //correctly logs the index 1 round for spling (2nd round) 
      var round = data["funding_rounds"][i]; 
      console.log('round' + i); //only logs round0, never loops around for round1, round2 
      for (i=0; i < investments.length; i++) { 

       var angelObject = round["investments"][i]["person"]; 
       if (angelObject != null) { 
        console.log("angel fired"); //fires for "Mitch Blumenfeld" 
        var angel = angelObject["first_name"] + " " + angelObject["last_name"]; 
        finalInvestorList[i] = angel; 
       } 

       var financialOrgObject = round["investments"][i]["financial_org"]; 
       if (financialOrgObject != null) { 
        console.log("financial_org fired"); //fires for "Bucknell Venture Plan Competition" 
        console.log(financialOrgObject['name']); //Bucknell VPC 
        var financialOrg = financialOrgObject["name"] 
        finalInvestorList[i] = financialOrg 
       } 

       var companyObject = round['investments'][i]["company"]; 
       if (companyObject != null) { 
        console.log('company fired'); //i haven't bothered with this yet.. just logging it so ill know if its firing 
       } 
      } 
     } 
     console.log(finalInvestorList); //["Bucknell Venture Plan Competition", "Mitch Blumenfeld"] 

    } 

JSON 객체이다. JSON 응답 데이터 내부의 객체는 이 데이터는 위기 API를 사용하여 검색하고 http://api.crunchbase.com/v/1/company/spling.js

"funding_rounds": 
    [{"round_code": "seed", 
    "source_url": "", 
    "source_description": "", 
    "raised_amount": 50000.0, 
    "raised_currency_code": "USD", 
    "funded_year": 2011, 
    "funded_month": 2, 
    "funded_day": 1, 
    "investments": 
    [{"company": null, 
     "financial_org": 
     {"name": "Bucknell Venture Plan Competition", 
     "permalink": "bucknell-venture-plan-competition", 
     "image": null}, 
     "person": null}, 
     {"company": null, 
     "financial_org": null, 
     "person": 
     {"first_name": "Mitch", 
     "last_name": "Blumenfeld", 
     "permalink": "mitch-blumenfeld", 
     "image": null}}]}, 
    {"round_code": "seed", 
    "source_url": "http://techcrunch.com/2011/09/08/dreamit-ventures-launches-its-fall-2011-philadelphia-class/", 
    "source_description": "", 
    "raised_amount": 25000.0, 
    "raised_currency_code": "USD", 
    "funded_year": 2011, 
    "funded_month": 9, 
    "funded_day": 1, 
    "investments": 
    [{"company": null, 
     "financial_org": 
     {"name": "DreamIt Ventures", 
     "permalink": "dreamit-ventures", 
     "image": 
      {"available_sizes": 
      [[[150, 
       57], 
       "assets/images/resized/0002/7773/27773v5-max-150x150.jpg"], 
      [[250, 
       96], 
       "assets/images/resized/0002/7773/27773v5-max-250x250.jpg"], 
      [[251, 
       97], 
       "assets/images/resized/0002/7773/27773v5-max-450x450.jpg"]], 
      "attribution": null}}, 
     "person": null}]}, 
    {"round_code": "a", 
    "source_url": "http://techcrunch.com/2011/12/05/new-content-sharing-network-spling-launches-announces-400k-series-a/", 
    "source_description": "New Content Sharing Network Spling Launches, Announces $400K Series A", 
    "raised_amount": 400000.0, 
    "raised_currency_code": "USD", 
    "funded_year": 2011, 
    "funded_month": 12, 
    "funded_day": 5, 
    "investments": 
    [{"company": null, 
     "financial_org": 
     {"name": "Deep Fork Capital", 
     "permalink": "deep-fork-capital-2", 
     "image": 
      {"available_sizes": 
      [[[150, 
       20], 
       "assets/images/resized/0008/0167/80167v1-max-150x150.png"], 
      [[250, 
       34], 
       "assets/images/resized/0008/0167/80167v1-max-250x250.png"], 
      [[450, 
       62], 
       "assets/images/resized/0008/0167/80167v1-max-450x450.png"]], 
      "attribution": null}}, 
     "person": null}, 
     {"company": null, 
     "financial_org": null, 
     "person": 
     {"first_name": "John", 
     "last_name": "Ason", 
     "permalink": "john-ason", 
     "image": null}}, 
     {"company": null, 
     "financial_org": null, 
     "person": 
     {"first_name": "Mitchell", 
     "last_name": "Blumenfeld", 
     "permalink": "mitchell-blumenfeld", 
     "image": null}}, 
     {"company": null, 
     "financial_org": null, 
     "person": 
     {"first_name": "Gianni", 
     "last_name": "Martire", 
     "permalink": "gianni-martire", 
     "image": 
      {"available_sizes": 
      [[[138, 
       150], 
       "assets/images/resized/0006/3720/63720v4-max-150x150.jpg"], 
      [[230, 
       250], 
       "assets/images/resized/0006/3720/63720v4-max-250x250.jpg"], 
      [[414, 
       450], 
       "assets/images/resized/0006/3720/63720v4-max-450x450.jpg"]], 
      "attribution": ""}}}]}] 

에서 전체 형태로 도움을 주셔서 감사를 발견 할 수있는 데이터 [ "funding_rounds"로 표시됩니다!

답변

3
각 루프 카운터 변수에 대해 동일한 이름을 사용

때문에 내부 루프가 완료되고, 외부 루프는 두번째 반복에 오면, iinvestments.length하지 1이다. 각 루프에 대해 서로 다른 변수 이름을 사용

또한
for (var roundIdx = 0; roundIdx < fundingRounds.length; roundIdx++) { 
    ... 
    for (var invIdx = 0; invIdx < investments.length; invIdx++) { 
     ... 

array[i] = value 표기법을 사용하여 배열을 채울 수없는, 단지 array.push(value)을 사용합니다. 색인을 추적 할 필요가 없습니다.

하지만, 당신의 배열을 반복 Array.forEach()을 사용하고 대괄호 표기법 대신 도트 구문을 사용하는 것이 좋습니다 것 :

기본적으로하지 않습니다 Array.forEach()을 지원하는 이전 버전의 브라우저를 들어
function investorList(data, num) { 
    var finalInvestorList = []; 
    data.funding_rounds.forEach(function (round, i) { 
     round.investments.forEach(function (investment, i) { 
      if (investment.person) { 
       finalInvestorList.push(investment.person.first_name + " " + 
             investment.person.last_name); 
      } 
      else if (investment.financial_org) { 
       finalInvestorList.push(investment.financial_org.name) 
      } 
      else if (investment.company) { 
       finalInvestorList.push(investment.company.name) 
      } 
     } 
    } 
} 

, 브라우저를 심은 할 the implementation here를 사용합니다.

+0

감사합니다. 항상 몇 시간 동안 꼼짝 않고 바라 보면서 두 번째 눈빛을 보게됩니다. 고마워요! –