【问题标题】:Hot to get PHP variables in JSON format and refresh by AJAX热门获取 JSON 格式的 PHP 变量并通过 AJAX 刷新
【发布时间】:2023-03-26 07:10:01
【问题描述】:

我有一个以 XML 格式导出数据的设备。 我想在我的网页上显示这些数据+通过 ajax 每秒刷新一次。

所以我的 PHP 文件是:

<?php
$xml=simplexml_load_file("http://admin:pass@192.168.0.109/st0.xml") or die("Error: Cannot create object");
echo json_encode($xml);
exit();

这将显示:

{"out0":"1","out1":"1","out2":"1","out3":"1","out4":"1","out5":"1 ","out6":"1","di0":"up","di1":"up","di2":"up","di3":"up","ia0":"473", "ia1":"166","ia2":"359","ia3":"187","ia4":"4326","ia5":"1832","ia6":"36","ia7 ":"198","ia8":"6","ia9":"234","ia10":"-600","ia11":"246","ia12":"-600","ia13 ":"0","ia14":"0","ia15":"65952","ia16":"0","ia17":"854000","ia18":"1000","ia19": "192","freq":"5008","duty":"500","pwm":"0","sec0":"27","sec1":"17","sec2":"20 ","sec3":"1","sec4":"1481894628"}

所以现在我知道如何显示整个数据并刷新,但我现在不知道如何将每个数据放在单独的 div 中,例如。只显示有趣的部分:

<!DOCTYPE html>
<html>
    <head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
        $(document).ready(function(){
            setInterval(function() {
                $("#load_results").load("get_xml.php");
            }, 1000);
        });

    </script>
    </head>
    <body>
        <div id = "load_results"></div>
    </body>
</html>

【问题讨论】:

  • 为什么不把它们放到一张桌子上呢?
  • 为什么要将 XML 转换为 JSON?只需传递 XML,将其加载到 jQuery 中并使用 jQuery Api 从中获取数据。
  • @Thw:其实你是对的,我不需要 JSON。只是不知道如何将 XML 传递给查询,然后显示特定的参数。

标签: php json ajax xml


【解决方案1】:

你有两种情况:

  1. 要么你知道 JSON 的格式,你就可以构建一个 DOM 使用 jQuery(例如)以您想要的确切格式构建结构 使用您拥有的确切数据。

为此,您必须访问 JSON 对象的对象属性,例如:

$("#load_results").load("get_xml.php");

变成:

$.get( "get_xml.php", function( data ) { // retrieves the JSON from that file using AJAX
    var parsedJSON = JSON.parse(data); // we convert the JSON string into a Javascript object

    var formattedString = 'Our OUT0 is ' + parsedJSON.out0 + '<br>'; // we access the data in the Javascript object as for all Javascript objects, see http://www.w3schools.com/js/js_objects.asp
    formattedString = formattedString + 'Our ia12 is ' + parsedJSON.ia12; // and so on

    $('#load_results').html(formattedString);
});
  1. 要么您不知道格式,要么 你根本不在乎。在这种情况下,情况更简单,你 可以为 JavaScript 使用 JSON 美化工具,例如 在这里实现:http://jsfiddle.net/unLSJ/(由我以外的其他人,为他干杯)。

对于选项 2,这将是库:

// Library
window.jsonPrettifier = {
   replacer: function(match, pIndent, pKey, pVal, pEnd) {
      var key = '<span class=json-key>';
      var val = '<span class=json-value>';
      var str = '<span class=json-string>';
      var r = pIndent || '';
      if (pKey)
         r = r + key + pKey.replace(/[": ]/g, '') + '</span>: ';
      if (pVal)
         r = r + (pVal[0] == '"' ? str : val) + pVal + '</span>';
      return r + (pEnd || '');
      },
   prettyPrint: function(obj) {
      var jsonLine = /^( *)("[\w]+": )?("[^"]*"|[\w.+-]*)?([,[{])?$/mg;
      return JSON.stringify(obj, null, 3)
         .replace(/&/g, '&amp;').replace(/\\"/g, '&quot;')
         .replace(/</g, '&lt;').replace(/>/g, '&gt;')
         .replace(jsonLine, library.json.replacer);
      }
   };

在代码中的某处添加此库后,您可以替换

$("#load_results").load("get_xml.php");

$.get( "get_xml.php", function( data ) { // retrieves the JSON from that file using AJAX
  var parsedJSON = JSON.parse(data);
  $('#load_results').html(window.jsonPrettifier.prettyPrint(parsedJSON));
});

你会得到一个漂亮的打印 JSON。

【讨论】:

  • 感谢 cmets,但在我的情况下,我只需要显示部分数据。前任。 out1 和 out2 和 pwm,所以我想显示特定的变量。在 PHP 中我可以调用数组中的元素,但我不知道如何在 JS/jquery 中调用
  • 所以问题是你想像选项1一样访问JSON对象。我已经扩展了选项1的解释,希望它有意义。
  • 为什么你不能只在 php 文件中进行数据摆弄/显示并 ajax 结果?
  • 这是一个偏好问题,不像问的人说他想使用php作为他的模板引擎/输出生成器。
  • 但他也可能不知道
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多