Christophermp
Christophermp

Reputation: 186

How to save JSON data to a variable in JavaScript

I'm trying to display ping information in a html page that is presented in JSON format. I have some what achived it but i can't figure out how to print it out in an ordered way.

JS Script

function scanner() {
  var evilscan = require("evilscan");

  var options = {
    target: "10.0.0.161",
    port: "21-23",
    status: "TROU", // Timeout, Refused, Open, Unreachable
    banner: true
  };

  var scanner = new evilscan(options);

  scanner.on("result", function(data) {
    // fired when item is matching options
    console.log(data);
    document.getElementById("pingStatus").innerHTML = JSON.stringify(
      data,
      undefined,
      2
    );
  });

  scanner.on("error", function(err) {
    throw new Error(data.toString());
  });

  scanner.on("done", function() {
    // finished !
  });

  scanner.run();
}

I have taken some inspiration from W3 Schools https://www.w3schools.com/js/tryit.asp?filename=tryjson_parse

Added following to HTML:

<p id="pingStatus" type="text"></p>

But are presented with a JSON string that looks like:

{ "ip": "10.0.0.161", "port": 21, "banner": "", "status": "closed (timeout)" }

What is the best/common way to handle and display strings like this on a HTML page so its displayed like:

Example

ect..

Upvotes: 3

Views: 1529

Answers (4)

ellipsis
ellipsis

Reputation: 12152

You can use a forEach loop and set the innerHTML of that element using .textContent property

var a = {
  "ip": "10.0.0.161",
  "port": 21,
  "banner": "",
  "status": "closed (timeout)"
};

var ele = document.getElementById("pingStatus");
Object.values(a).forEach(e => e!=''?ele.innerHTML += '<li>'+e+'</li>':false)
<p id="pingStatus" type="text"></p>

Upvotes: 4

Birbal Singh
Birbal Singh

Reputation: 1072

var txt = '{"name":"John", "address" : [{"c":"1"}], "age":30, "city":"New York"}';


document.getElementById("demo").innerHTML = "<pre>"+JSON.stringify(JSON.parse(txt),undefined, 2) +"</pre>";
<div id="demo" ></div>

We can use json object to show on html with 'pre' tag

var txt = '{"name":"John", "address" : [{"c":"1"}], "age":30, "city":"New York"}'
document.getElementById("demo").innerHTML = "<pre>"+JSON.stringify(JSON.parse(txt),undefined, 2) +"</pre>";

Upvotes: 1

Gowrishankar R
Gowrishankar R

Reputation: 21

This will help you

for (x in data) {
  txt += "<p>" + data[x]. ip + "</p>" + "<p>" + data[x]. port + "</p>";
}

Upvotes: 1

Ng Sek Long
Ng Sek Long

Reputation: 4786

You shouldn't do JSON.stringify on your data, instead just use the data as is.

First step:

Add more html:

<p id="ip" type="text"></p>
<p id="port" type="text"></p>
<p id="pingStatus" type="text"></p>

Seconds Step:

Change your JavaScript as follows:

scanner.on("result", function(data) {
      // fired when item is matching options
      console.log(data);
      document.getElementById("ip").innerHTML = data.ip;
      document.getElementById("port").innerHTML = data.port;
      document.getElementById("pingStatus").innerHTML = data.status;
});

Upvotes: 2

Related Questions