No JSON data displayed on php script
本问题已经有最佳答案,请猛点这里访问。
我有json数据,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | { "firmware": [ { "Request":"/Firmware/samsung", "Count":"2954" }, { "Request":"/Firmware/apple", "Count":"2954" } ], "exe": [ { "Request":"/applications/appstore.exe", "Count":"4482" } ] } |
我试图通过php脚本读取并显示它。但没有看到任何数据。脚本如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <script type="text/javascript" src="http://localhost/json/softwarelogs.js"> <fieldset class="light" style="width: 60%; margin-right: 20px; margin-bottom: 20px; float: left;"> <legend>Top Firmware Downloads</legend> </fieldset> <fieldset class="light" style="width: 60%; margin-right: 20px; margin-bottom: 20px; float: left;"> <legend>Top EXE Downloads</legend> </fieldset> var htmlString =""; $.each(firmware, function(i, item) { htmlString = htmlString +" <li> " + item.Request; }); $('#topFirmware').html(htmlString +""); htmlString =""; $.each(exe, function(i, item) { htmlString = htmlString +" <li> " + item.Request; }); $('#exe').html(htmlString +""); $.fn.digits = function(){ return this.each(function(){ $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g,"$1,") ); }) |
我对PHP编程还比较陌生,所以不确定它的排列和语法。如果有什么问题,如果您需要更多信息,请纠正我并指导我。事先谢谢!!
您不能通过脚本标记直接加载(和使用)JSON数据,请尝试以下方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | $.getJSON('/json/softwarelogs.js', function(data) { var htmlString =""; $.each(data.firmware, function(i, item) { htmlString = htmlString +" <li> " + item.Request; }); $('#topFirmware').html(htmlString +""); htmlString =""; $.each(exe, function(i, item) { htmlString = htmlString +" <li> " + item.Request; }); $('#exe').html(htmlString +""); $.fn.digits = function(){ return this.each(function(){ $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g,"$1,") ); }) }); |
假设您的JSON保存在一个变量中,比如
注意:如果它是一个简单的字符串,那么您可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var jsData={ "firmware": [ { "Request":"/Firmware/samsung", "Count":"2954" }, { "Request":"/Firmware/apple", "Count":"2954" } ], "exe": [ { "Request":"/applications/appstore.exe", "Count":"4482" } ] }; |
现在更改以下语句
1 | $.each(firmware, function(i, item) |
到
1 | $.each(jsData.firmware, function(i, item) |