How to solve PHP array with Json and Javascript -


can me problem ? trying display array retrieve 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();   ?> 

output

[{ ".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; < 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 way displaying data in table form format in html file

at least 2 problems there:

  1. 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 objects (after each } , before next {).

    don't generate json manually. instead, build array of want send in php, use json_encode handles details you.

  2. you're using properties on objects in array aren't in json. code usese arr[i].id, arr[i].chain, , arr[i].action, objects in json don't have id, chain, or action properties, have name, city, city2, , on.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -