洛丁山奈 2017-06-02 03:12 采纳率: 33.3%
浏览 3170
已采纳

json字符串在html中,怎么转换成表格展示?

{
"response":{
"numFound":"2",
"start":"0",
"docs":[
{
"id":"857400f2-b680-4de1-b9d1-5ea107f6f362",
"agentId":"ServicePlatform_Infrastructure_CPU-Usage",
"category":"Infrastructure",
"subCategory":"CPU-Usage",
"thirdCategory":"N/A",
"dataAcquistionTime":"1496332066849",
"optional":"",
"label":"CPU使用率",
"data":"98%",
"data_i":"98",
"data_f":"0",
"optionData_f":"0",
"option2Label":"",
"option2Data":"",
"option2Data_f":"0",
"option3Label":"",
"option3Data":"",
"option3Data_f":"0",
"seqNo":"0",
"_version_":"1569048833034289200"
}
]
}
}

 这样的json,在html怎么解析成表格?求代码
  • 写回答

7条回答 默认 最新

  • 全栈极简 博客专家认证 2017-06-02 03:34
    关注
     <html>
    <head>
        <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
        <script type="text/javascript">
            $(function(){
                var json ="{\"response\": {\"numFound\": \"2\",\"start\": \"0\",\"docs\": [{\"id\": \"857400f2-b680-4de1-b9d1-5ea107f6f362\",\"agentId\": \"ServicePlatform_Infrastructure_CPU-Usage\",\"category\": \"Infrastructure\",\"subCategory\": \"CPU-Usage\",\"thirdCategory\": \"N/A\",\"dataAcquistionTime\": \"1496332066849\",\"optional\": \"\",\"label\": \"CPU使用率\",\"data\": \"98%\",\"data_i\": \"98\",\"data_f\": \"0\",\"optionData_f\": \"0\",\"option2Label\": \"\",\"option2Data\": \"\",\"option2Data_f\": \"0\",\"option3Label\": \"\",\"option3Data\": \"\",\"option3Data_f\": \"0\",\"seqNo\": \"0\",\"_version_\": \"1569048833034289200\"}]}}";
    
                var elements = JSON.parse(json).response.docs;
                for(var i = 0;i<elements.length;i++){
                    $("#tt").append("<tr>");
                    $("#tt").append("<td width='5%'>"+elements[i].id+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].agentId+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].category+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].subCategory+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].thirdCategory+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].dataAcquistionTime+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].optional+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].label+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].data+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].data_i+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].data_f+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].optionData_f+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].option2Label+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].option2Data+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].option2Data_f+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].option3Label+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].option3Data+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].option3Data_f+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i].seqNo+"</td>");
                    $("#tt").append("<td width='5%'>"+elements[i]._version_+"</td>");
                    $("#tt").append("</tr>");
                }
            });
    
        </script>
    </head>
    <body>
        <table id="tt" border="1" cellspacing="0" cellpadding="0">
        </table>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决