2014-09-30 167 views
0

感謝您查看我的問題。我有兩個解析類Game和CricketScore。遊戲擁有CricketScore課程的「first_inning_score」和「second_inning_score」指針。使用嵌套對象解析解析對象-Javascript

我只得到CricketScore的指針,如下所示。 如何從CricketScore獲取實際數據?(已解決)。現在的問題是如何在車把中顯示數據?

我正在從parse這個JSON對象(更新

{ 
    "results": [ 
     { 
      "first_inning_score": { 
       "overs": "0", 
       "parent": { 
        "__type": "Pointer", 
        "className": "Game", 
        "objectId": "Yibf5wHLIM" 
       }, 
       "team_name": "Fr11", 
       "wickets": "0", 
       "innings": "1", 
       "runs": "0", 
       "createdAt": "2014-09-30T18:27:36.640Z", 
       "updatedAt": "2014-09-30T18:27:36.640Z", 
       "objectId": "0NhGUYySg5", 
       "__type": "Object", 
       "className": "CricketScore" 
      }, 
      "second_inning_score": { 
       "wickets": "0", 
       "innings": "2", 
       "runs": "0", 
       "team_name": "Rb11", 
       "overs": "0", 
       "parent": { 
        "__type": "Pointer", 
        "className": "Game", 
        "objectId": "Yibf5wHLIM" 
       }, 
       "createdAt": "2014-09-30T18:27:36.760Z", 
       "updatedAt": "2014-09-30T18:27:36.760Z", 
       "objectId": "4gHyHO84Ep", 
       "__type": "Object", 
       "className": "CricketScore" 
      }, 
      "city": "Poway", 
      "elected": "bowl", 
      "groundname": "4s RANCH", 
      "hometeam": "Rb11", 
      "maxovers": "20", 
      "score_moderator": [ 
       { 
        "__type": "Pointer", 
        "className": "_User", 
        "objectId": "00hBJbvgmu" 
       } 
      ], 
      "sport": "Cricket", 
      "status": "inning1", 
      "team1": "Fr11", 
      "team2": "Rb11", 
      "toss": "Rb11", 
      "createdAt": "2014-09-30T18:27:36.504Z", 
      "updatedAt": "2014-09-30T18:27:36.898Z", 
      "objectId": "Yibf5wHLIM" 
     } 
    ] 
} 

車把代碼

<script type="text/x-handlebars-template" id="single-quote-template"> 
    <!-- Feed Entry --> 
    <div class="row singleQuote"> 
     <div class="panel" align="center"> 
     <p><h4><strong>   {{team1}} vs {{team2}}</strong></h4></p> 
     <p>Toss won by {{toss}} elected to {{elected}}, {{createdAt}}, {{groundname}}</p> 
     <p>{{first_inning_score.wickets}}</p> // ** <<---this is not working**** 

     </div> 
    </div> 
    <hr /> 
    </script> 

JavaScript代碼。我不知道如何獲得蟋蟀的成績。我無法關注Dehli的答案。

query.find({ 
    success: function(results) { 
     // The query was successful, and has passed 
     // back an array of PFObjects for you to use 

     // Since we're appending, clear the list out 
     // every time we're about to add results 
     $("#quoteList").html(""); 

     // Compile the Handlebars template we're going 
     // to stick the results into. Pass Handlebars the 
     // ID of the script tags in index.html that contain 
     // the template. 
     var template = Handlebars.compile($("#single-quote-template").html()); 

     // Iterate over all the results 
     $(results).each(function(i,e) { 
     // Serialize the PFObject and store it in q 
     var q = e.toJSON(); 
     // Select the DOM element we're appending to, 
     // Then append the template, passing in q to 
     // provide the values of the template variables 
     $("#quoteList").append(template(q)); 
     }); 
    }, 
    error: function(error) { 
     if (error.message == "unauthorized") { 
     // Temporary message if you haven't added your own credentials for Parse.com yet. Remove once set up. 
     console.warn("Please fill in your own Parse.com App ID and Javascript Key on line 107 of index.html"); 
     } 
     // Handle the error here 
    } 
    }) 
+0

您的查詢是什麼樣子的?嘗試添加'query.include(「first_inning_score」);''和'query.include(「second_inning_score」);' – Dehli 2014-09-30 19:05:55

+0

非常感謝!這工作。你能告訴我怎樣才能把第一局得分數據處理吧?查看上面更新的代碼 – scoretickr 2014-09-30 19:17:39

回答

0

您需要做的是將以下內容添加到您的JavaScript查詢中。

query.include("first_inning_score"); 
query.include("second_inning_score"); 
+0

非常感謝您的回覆。你能解釋一下嗎?「然後提取出分數,解析出每個分數並查看相應的鍵」。我是新手。 – scoretickr 2014-09-30 19:36:31

+0

讓我知道這是否有效。 @ user3470818 – Dehli 2014-09-30 19:44:13

+0

嗨德赫利,非常感謝您抽出時間。我無法理解將代碼放在我的代碼中的位置。我粘貼了我的JS查詢Parse。我從解析中得到結果Json對象。我能夠訪問諸如createdAt,team1,team2等一級數據,但不能first_inning_score。最終它的目的是顯示數據在我的html文件的句柄中。你能告訴我如何循環瀏覽JSON嗎? – scoretickr 2014-09-30 21:00:46