【问题标题】:Display JSON Object values to a table将 JSON 对象值显示到表中
【发布时间】:2016-01-20 18:36:21
【问题描述】:

我的 Json Array 格式,我尝试在 html 表格中显示 locTable 中的值

"categoryName": [
"Construction",
"Emergency Response",
"Equipment",
"General",
"General Safety",
"Marine",
"Materials",
"Products",
"Protection of Workers",
"Transport",
"Workplace"
],
"locTable": [
{
  "evaluationPercent": 100,
  "relevancePercent": 100,
  "category": "Workplace",
  "ldloc": []
},
{
  "evaluationPercent": 100,
  "relevancePercent": 100,
  "category": "Workplace",
  "ldloc": []
},
{
  "evaluationPercent": 100,
  "relevancePercent": 100,
  "category": "Workplace",
  "ldloc": []
},

我的html代码

 <table  class="red" style="width: 100%; height: 100%;">
 <thead>
 <tr>
 <th>Category</th>
 <th class="right">Rele Completed</th>
 <th class="right">Eval Completed</th>
 </tr>
 </thead>
 <tbody id="display" class="no-border-x">
 </tbody>
 </table>

我尝试使用 jQuery 迭代 Json 对象,但无法显示正确的值,我的代码结果显示未定义。我尝试获取结果的代码。

 var trHTML = '';
 jQuery.each(json.locTable, function (k, item) {                   
 trHTML += '<tr><td>' + item.category + '</td><td>'       
 +item.evaluationPercent + '</td><td>' +item.relevancePercent +
 '</td></tr>';
 });
 jQuery('#display').append(trHTML);

【问题讨论】:

    标签: javascript jquery json gson


    【解决方案1】:

    我不知道您是否没有发布整个 JSON,但就目前而言,它是无效的。这是有效的:

    var json = {
      "categoryName": [
        "Construction",
        "Emergency Response",
        "Equipment",
        "General",
        "General Safety",
        "Marine",
        "Materials",
        "Products",
        "Protection of Workers",
        "Transport",
        "Workplace"
      ],
      "locTable": [{
        "evaluationPercent": 100,
        "relevancePercent": 100,
        "category": "Workplace",
        "ldloc": []
      }, {
        "evaluationPercent": 100,
        "relevancePercent": 100,
        "category": "Workplace",
        "ldloc": []
      }, {
        "evaluationPercent": 100,
        "relevancePercent": 100,
        "category": "Workplace",
        "ldloc": []
      }]
    };
    

    它有效,请参阅:https://jsfiddle.net/ve7r4422/

    【讨论】:

      【解决方案2】:

      乍一看,我觉得xml格式有问题。

      有几个帖子可能会回答您的问题,如下所示:

      Convert JSON array to an HTML table in jQuery

      【讨论】:

        【解决方案3】:

        var json = {
          "locTable": [{
            "evaluationPercent": 80,
            "relevancePercent": 100,
            "category": "Workplace",
            "ldloc": []
          }, {
            "evaluationPercent": 90,
            "relevancePercent": 70,
            "category": "Workplace",
            "ldloc": []
          }, {
            "evaluationPercent": 100,
            "relevancePercent": 40,
            "category": "Workplace",
            "ldloc": []
          }]
        };
        
        jQuery('#display').append($.map(json.locTable, function(record){
          return '<tr><td>'+ record.category +'</td><td>'+ record.evaluationPercent +'</td><td>'+ record.relevancePercent +'</td></tr>';
        }));
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <table class="red" style="width: 100%; height: 100%;">
          <thead>
            <tr>
              <th>Category</th>
              <th class="right">Rele Completed</th>
              <th class="right">Eval Completed</th>
            </tr>
          </thead>
          <tbody id="display" class="no-border-x"></tbody>
        </table>

        【讨论】:

          猜你喜欢
          • 2018-07-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-20
          • 2013-07-27
          • 2020-06-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多