【问题标题】:Parse object with nested object parsing- Javascript使用嵌套对象解析来解析对象 - Javascript
【发布时间】:2014-09-30 18:59:53
【问题描述】:

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

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

我从 parse(updated) 获取这个 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
    }
  })

【问题讨论】:

  • 您的查询是什么样的?尝试添加query.include("first_inning_score");query.include("second_inning_score");
  • 非常感谢!!!!这已经奏效了。你能告诉我如何将第一局得分的数据放到手柄上吗?请参阅上面的更新代码

标签: javascript json serialization parse-platform


【解决方案1】:

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

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

【讨论】:

  • 非常感谢您的回复。您能否解释一下“然后提取分数,解析出每个分数并查看相应的键”。我是新手。
  • 您好 Dehli,非常感谢您抽出宝贵时间。我无法理解将您的代码放在我的代码中的哪个位置。我已经为 Parse 粘贴了我的 JS 查询。我从解析中获取结果 Json 对象。我能够访问一级数据,例如 createdAt、team1、team2 等。但不能访问 first_inning_score。最终目标是在我的 html 文件中的车把中显示数据。你能告诉我如何循环遍历 JSON 吗?
  • 你能用这个替换检票口吗:first_inning_score.length
  • 我用 HTML 替换它,没有任何反应。
  • 只有:first_inning_score 有什么表现吗?
猜你喜欢
  • 2015-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多