在表的标题中加载JSON数据

时间:2017-03-19 13:23:21

标签: javascript html json

我在将我的JSON数据加载到表格标题时遇到问题。在我的函数loadJSON中,我是这样加载一个对象的注释:



<tr title="this['achievementlist'][i].comment"></tr>
&#13;
&#13;
&#13;

导致: link of picture

当我删除&#34;。注释已加载,但只加载第一个单词。 看完之后:HTML title attribute in Rails displaying only first word 我添加了&#34;回来。逻辑上这又导致了link of picture

如何加载对象&#39;成就列表&#39;的JSON评论?作为标题?

JSON数据:

&#13;
&#13;
{
"gijs":[
    {
      "name": "gijs",
      "walletpoints": "5",
      "totalpoints": "5",
      "achievementlist":[
      {
        "achievementname": "een comaan",
        "points": "50",
        "comment": "werkt voor geen meter en ziet er niet uit"
      },
      {
        "achievementname": "meer dan 5 huisdieren",
        "points": "40",
        "comment": "allemaal muizen in de kelder"
      }
}
&#13;
&#13;
&#13;

我的所有HTML代码:

&#13;
&#13;
<table>
                        <tr>
                            <td class="achievements"></td>
                            <script>
                                $.getJSON("../json/package.json", function (data)
                                {
                                    $.each(data.gijs, function ()
                                    {
                                        for (var i=0; i<this['achievementlist'].length; i++)
                                        {
                                            $(".achievements").append("<tr title="+"this['achievementlist'][i].comment" +" class='tablerow'><td class='JSONDATA'><div class='JSONName'>" + this['achievementlist'][i].achievementname + "</div><div class='JSONPoints'>Points:   " + "<strong>"+this['achievementlist'][i].points+"</strong>" + "</div></td></tr>");
                                        }
                                    });
                                });
                            </script>
                        </tr>
                    </table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

var data = {
"gijs":[
    {
      "name": "gijs",
      "walletpoints": "5",
      "totalpoints": "5",
      "achievementlist":[
      {
        "achievementname": "een comaan",
        "points": "50",
        "comment": "werkt voor geen meter en ziet er niet uit"
      },
      {
        "achievementname": "meer dan 5 huisdieren",
        "points": "40",
        "comment": "allemaal muizen in de kelder"
      }]
}]
}

$.each(data.gijs, function (){
for (var i=0; i<this['achievementlist'].length; i++){
$(".achievements").append('<tr title="'+this['achievementlist'][i].comment+'" class="tablerow"><td class="JSONDATA"><div class="JSONName">'+ this['achievementlist'][i].achievementname + '</div><div class="JSONPoints">Points <strong>'+this['achievementlist'][i].points+'</strong></div></td></tr>');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
                        <tr>
                            <td class="achievements"></td>
                        </tr>
                    </table>