如何使用 Json 和 Javascript 解决 PHP 数组
How to solve PHP array with Json and Javascript
有人可以帮助我解决这个问题吗?我正在尝试以HTML/Java形式显示我从PHP检索的数组。
<?php
header("Content-Type: application/json; charset=UTF-8");
require('routeros_api.class.php');
$API = new routeros_api();
$API->debug = true;
$API->connect('1.1.1.1', 'admin', '123');
$API->write('/ip/firewall/filter/print');
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
echo json_encode ($ARRAY);
$API->disconnect();
?>
输出
[{ ".id":"*6", "链":"未使用的HS链", "动作":"直通", "日志":"假", "禁用":"真"},
{ ".id":"*5", "链":"输入", "操作":"接受", "日志":"假", "禁用":"真"},
{ ".id":"*2A", "链":"未使用的HS链", "动作":"下降", "日志":"假", "禁用":"真"}]
显示Java.html
<tbody id="myTable">
<script>
var xmlhttp = new XMLHttpRequest();
var url = "testdata2.php";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("get", url, true);
xmlhttp.send();
function myFunction(response) {
var arr = JSON.parse(response);
var i;
var outp = "<tbody>";
for(i = 0; i < arr.length; i++) {
outp += "<tr>" +
"<td>" + arr[i].id + "</td>" +
"<td>" + arr[i].chain + "</td>" +
"<td>" + arr[i].action + "</td>" +
"<td>" + arr[i].log + "</td>" +
"<td>" + arr[i].disabled + "</td>" +
"</tr>";
}
outp += "</tbody>"
document.getElementById("myTable").innerHTML = outp;
}
</script>
</tbody>
顺便说一下,我在html文件中以表格形式显示数据
那里至少有两个问题:
-
您正在输出无效的 JSON:
[ { "Name": "*6", "City": "unused-hs-chain", "City2": "unused-hs-chain", "City3": "unused-hs- chain", "Country": "passthrough" }{ "Name": "*5", "City": "input", "City2": "input", "City3": "input", "Country": "accept" }{ "Name": "*2A", "City": "unused-hs-chain", "City2": "unused-hs-ch
您需要在这些对象之间使用逗号(在每个
}
之后和下一个{
之前)。请勿手动生成 JSON。相反,只需在 PHP 中构建一个您想要发送回的内容数组,然后使用
json_encode
以便它为您处理细节。 -
你正在使用数组中不在 JSON 中的对象的属性。您的代码包含
arr[i].id
、arr[i].chain
和arr[i].action
,但 JSON 中的对象没有id
、chain
或action
属性,它们具有Name
、City
、City2
等。
为什么不直接使用 json_encode()
而不是手动构建它。
$output = array();
foreach($ARRAY as $rs) {
$output[] = array(
'Name' => $rs['id'],
'City' => $rs['chain'],
'City2' => $rs['chain'],
'City3' => $rs['chain'],
'Country' => $rs['action'],
);
}
echo json_encode($output);
exit;
您的 JSON 字符串响应现在缺少逗号{},
。
不要使用 json_encode() 函数手动构建,
上面的代码是
<?php
header("Content-Type: application/json; charset=UTF-8");
require('routeros_api.class.php');
$API = new routeros_api();
$API->debug = true;
$API->connect('1.1.1.1', 'admin', '123');
$API->write('/ip/firewall/filter/print');
$READ = $API->read(false);
$ARRAY = $API->parse_response($READ);
$outp = array();
foreach($ARRAY as $rs) {
$outp[] = array(
'Name' => $rs['id'],
'City' => $rs['chain'],
'City2' => $rs['chain'],
'City3' => $rs['chain'],
'Country' => $rs['action'],
);
}
$API->disconnect();
echo json_encode($outp);
?>
相关文章:
- PHP 强制下载并刷新解决方案不起作用
- 在php中,内联<脚本>解决
- 未解决的 PHP 页面刷新而无需 Ajax
- 需要 PHP 中 jQuery 滑块的帮助来解决每个问题
- 如何将誓言令牌推送到本地存储或本地会话并监听存储事件?(SoundCloud Php/JS 错误解决方法)
- 自定义 PHP/JS 分析解决方案问题
- socket.io + php + 这是目前现实的解决方案
- 如何使用 Json 和 Javascript 解决 PHP 数组
- 在PHP中使用JS来解决if window.width问题
- PHP - 从 PDF 读取用户选择的文本的解决方法
- 使用谷歌地图进行地理编码,没有OVER_QUERY_LMIT错误-尝试使用PHP解决方案
- 查看如何使用php脚本解决403错误
- 用于PHP的AJAX分页解决方案
- PHP导航到前一页后登录的解决方案
- 如何解决这个PHP Ajax阿拉伯字符编码问题
- 如何解决跨域问题,不使用php
- PHP AJAX文件上传器解决方案
- 将html表导出到可下载的excel中,并解决PHP问题
- PHP 随机文本 - 是否有更好、更干净或更少服务器密集型的解决方案
- PHP返回按钮的终极HTTP_REFERER解决方案