admin管理员组文章数量:1425674
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 .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/mon way to handle and display strings like this on a HTML page so its displayed like:
Example
- ip: 10.0.0.161
- Port: 21
ect..
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./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/mon way to handle and display strings like this on a HTML page so its displayed like:
Example
- ip: 10.0.0.161
- Port: 21
ect..
Share Improve this question edited Feb 20, 2019 at 7:32 Prasanth Ganesan 5514 silver badges14 bronze badges asked Feb 20, 2019 at 7:18 ChristophermpChristophermp 1861 gold badge4 silver badges14 bronze badges 1- you will have to use sth like a template engine where you will pass the object (not the json string) and print the fields the way you want it. If you dont want to use a template engine for only this simple issue, then simply write a js function that accepts object data and returns html string appropriately formatted – Nikos M. Commented Feb 20, 2019 at 7:22
4 Answers
Reset to default 4You 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>
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;
});
This will help you
for (x in data) {
txt += "<p>" + data[x]. ip + "</p>" + "<p>" + data[x]. port + "</p>";
}
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>";
本文标签: How to save JSON data to a variable in JavaScriptStack Overflow
版权声明:本文标题:How to save JSON data to a variable in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745360991a2655292.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论