Reputation: 69
Can anybody help me with this problem ? I am trying to display the array that I retrieve from PHP in HTML / Java form.
<?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", "chain":"unused-hs-chain", "action":"passthrough", "log":"false", "disabled":"true"},
{ ".id":"*5", "chain":"input", "action":"accept", "log":"false", "disabled":"true"},
{ ".id":"*2A", "chain":"unused-hs-chain", "action":"drop", "log":"false", "disabled":"true"}]
displayjava.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>
By the way I am displaying the data in a table form format in html file
Upvotes: 0
Views: 127
Reputation: 145
Don't build manually use json_encode() function ,
Above code is
<?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);
?>
Upvotes: 1
Reputation: 1074158
At least two problems there:
You're outputting invalid 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
You need commas between those objects (after each }
and before the next {
).
Don't generate the JSON manually. Instead, just build up an array of what you want to send back in PHP, then use json_encode
so it handles the details for you.
You're using properties on the objects in the array that aren't in the JSON. Your code usese arr[i].id
, arr[i].chain
, and arr[i].action
, but the objects in your JSON don't have id
, chain
, or action
properties, they have Name
, City
, City2
, and so on.
Upvotes: 3
Reputation: 41885
Why not just use json_encode()
instead of building it manually.
$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;
Your JSON string response right now has missing {},
commas.
Upvotes: 1