使用嵌套对象解析来解析对象 - Javascript

时间:2014-09-30 18:59:53

标签: javascript json serialization parse-platform

感谢您查看我的问题。我有两个解析类Game和CricketScore。游戏有“first_inning_score”和“second_inning_score”指向CricketScore类的指针。

我只是指向CricketScore,如下所示。 如何从CricketScore获得实际数据?(已解决)。现在的问题是如何在车把中显示数据?

我从解析(更新

获取此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代码。我不知道如何获得cricketscores。我无法在下面跟随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
    }
  })

1 个答案:

答案 0 :(得分:0)

您需要做的是将以下内容添加到JavaScript查询中。

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