打破JSON对象

时间:2015-12-14 05:11:00

标签: javascript jquery

我正在尝试打破JSON对象。这不起作用,请指导。

var mock_data = {
  "available": [{
    "UserID": 7,
    "UserName": "Manoj",
    "Language": "Java",
    "Score": 9,
    "TimeLimit": 4.0
  }, {
    "UserID": 5,
    "UserName": "Thisun",
    "Language": "Java",
    "Score": 9,
    "TimeLimit": 4.0
  }, {
    "UserID": 6,
    "UserName": "Isuru",
    "Language": "Java",
    "Score": 7,
    "TimeLimit": 4.0
  }, {
    "UserID": 1,
    "UserName": "UserName LastName",
    "Language": "Java",
    "Score": 6,
    "TimeLimit": 6.0
  }],
  "selected": [{
    "UserID": 7,
    "UserName": "Manoj",
    "Language": "Java",
    "Score": 9,
    "TimeLimit": 4.0
  }, {
    "UserID": 5,
    "UserName": "Thisun",
    "Language": "Java",
    "Score": 9,
    "TimeLimit": 4.0
  }]
};

$(mock_data.available).each(function(i, value) {

  $('.risktable').append('<tr>' +
    '<td>' + (i + 1) + '</td>' +
    '<td>' + value.UserName + '</td>' +
    '<td>' + value.Language + '</td>' +
    '<td>' + value.Score + '</td>' +
    '<td>' + value.TimeLimit + '</td>' +
    '</tr>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="risktable">

</table>

以上不会产生任何输出。循环有什么问题吗?

1 个答案:

答案 0 :(得分:1)

http://codepen.io/anon/pen/advjeN 我尝试了你的代码工作。

你把Jquery CDN?如果没有,请将其放在标题中:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

您是否将代码放入jquery函数中?如果没有:

$(document).ready(function(){
    [YOUR CODE HERE]

});