简体   繁体   English

需要帮助以HTML显示JSON文件

[英]Need help displaying a JSON file in HTML

I recently received a VPS from DigitalOcean and I have been trying to make a page that will display the status of the VPS. 我最近从DigitalOcean收到了VPS,并且我一直试图制作一个页面来显示VPS的状态。 DigitalOcean has a very good API but i dont know where to get started. DigitalOcean有一个非常好的API,但我不知道从哪里开始。

My json file goes like this: 我的json文件是这样的:

{"status":"OK","droplets":[{"id":00000,"name":"JamieDuke","image_id":0000,"size_id":00,"region_id":4,"backups_active":false,"ip_address":"0.0.0.0","private_ip_address":null,"locked":false,"status":"active","created_at":"2014-04-21T23:45:51Z"}]}

UPDATE: I havent tried any code. 更新:我还没有尝试任何代码。 i have been looking high and low for something that works. 我一直在寻找一些可行的东西。 Also, the json code i showed up there is pulled from a webpage and has to be to be live UPDATE 2: I am a total newb when it comes to css and Javascript. 另外,我在那里显示的json代码是从网页中提取的,必须是实时更新2:关于CSS和Javascript,我是个新手。 the only language i can do is HTMl. 我唯一能做的语言是HTMl。 Can someone please help me with the other parts that are needed? 有人可以帮我其他需要的部分吗? I really need this script for a project. 我真的需要一个项目的脚本。

Also, i need it to pull the script that is pulled from a url. 另外,我需要它来拉出从URL提取的脚本。

What coding languages can you use? 您可以使用哪些编码语言? I would recommend either PHP or JavaScript, depending on where you want to put the file (your desktop or a web server). 根据您要放置文件的位置(台式机或Web服务器),我建议使用PHP或JavaScript。

A general solution: 通用解决方案:

  1. Parse the JSON into an Array/Object 将JSON解析为数组/对象
  2. Render HTML from the values in the Array/Object 从数组/对象中的值渲染HTML

Let us see some code you have tried! 让我们看看您尝试过的一些代码!

I would just use jquery and parse it. 我只会用jquery并解析它。 See the url below. 请参阅下面的网址。

https://api.jquery.com/jQuery.parseJSON/ https://api.jquery.com/jQuery.parseJSON/

It's as simple as 就这么简单

var data = JSON.parse(jsonString);
var html = [], i =0; ln = data.droplets.length;

html.push('<div class="style-div">data.status</div>');
for(i=0; i<ln;i++)
{
  var droplet = droplet[i];
  for(var prop in droplet) {
    html.push('<div class="style-droplet-inner">'+droplet[prop]+'</div>');
  }
}
body.innerHTML = html.join('');

You only need to worry about the CSS now. 您现在只需要担心CSS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM