我有一个本地组,大约有50台计算机,它们在运行Open Hardware Monitor,并将JSON文件输出到http://theirip:8085/data.json。我将某些部分的文本分别重命名为CPU,RAM,GPU,HDD。我正在尝试获取类似Value的东西,Value是CPU Core 1的子代,而Core 1是Temperatures的子代,CPU是CPU的子代。但是我只是想不通。
我还有一些其他代码可以加载从示例中复制的json(我可以在控制台中看到)。
我需要修改它以仅获取每个CPU和GPU的温度值。 RAM使用百分比和HDD使用百分比。 这是我无法解决的问题,因为我无法弄清楚仅输出JSON的这些部分的具体方法。
示例JSON:
{
"id": 0,
"Text": "Sensor",
"Children": [{
"id": 1,
"Text": "DESKTOP-PEJT98F",
"Children": [{
"id": 2,
"Text": "Lenovo 20EV0013MB",
"Children": [],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/mainboard.png"
}, {
"id": 3,
"Text": "CPU",
"Children": [{
"id": 4,
"Text": "Clocks",
"Children": [{
"id": 5,
"Text": "Bus Speed",
"Children": [],
"Min": "100 MHz",
"Value": "100 MHz",
"Max": "100 MHz",
"ImageURL": "images/transparent.png"
}, {
"id": 6,
"Text": "CPU Core #1",
"Children": [],
"Min": "500 MHz",
"Value": "600 MHz",
"Max": "2800 MHz",
"ImageURL": "images/transparent.png"
}, {
"id": 7,
"Text": "CPU Core #2",
"Children": [],
"Min": "500 MHz",
"Value": "600 MHz",
"Max": "2800 MHz",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/clock.png"
}, {
"id": 8,
"Text": "Temperatures",
"Children": [{
"id": 9,
"Text": "CPU Core #1",
"Children": [],
"Min": "31,0 °C",
"Value": "33,0 °C",
"Max": "58,0 °C",
"ImageURL": "images/transparent.png"
}, {
"id": 10,
"Text": "CPU Core #2",
"Children": [],
"Min": "32,0 °C",
"Value": "34,0 °C",
"Max": "63,0 °C",
"ImageURL": "images/transparent.png"
}, {
"id": 11,
"Text": "CPU Package",
"Children": [],
"Min": "34,0 °C",
"Value": "37,0 °C",
"Max": "63,0 °C",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/temperature.png"
}, {
"id": 12,
"Text": "Load",
"Children": [{
"id": 13,
"Text": "CPU Total",
"Children": [],
"Min": "0,0 %",
"Value": "3,1 %",
"Max": "100,0 %",
"ImageURL": "images/transparent.png"
}, {
"id": 14,
"Text": "CPU Core #1",
"Children": [],
"Min": "0,0 %",
"Value": "2,3 %",
"Max": "100,0 %",
"ImageURL": "images/transparent.png"
}, {
"id": 15,
"Text": "CPU Core #2",
"Children": [],
"Min": "0,0 %",
"Value": "3,9 %",
"Max": "100,0 %",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/load.png"
}, {
"id": 16,
"Text": "Powers",
"Children": [{
"id": 17,
"Text": "CPU Package",
"Children": [],
"Min": "1,2 W",
"Value": "1,2 W",
"Max": "12,6 W",
"ImageURL": "images/transparent.png"
}, {
"id": 18,
"Text": "CPU Cores",
"Children": [],
"Min": "0,2 W",
"Value": "0,2 W",
"Max": "10,9 W",
"ImageURL": "images/transparent.png"
}, {
"id": 19,
"Text": "CPU Graphics",
"Children": [],
"Min": "0,0 W",
"Value": "0,0 W",
"Max": "0,3 W",
"ImageURL": "images/transparent.png"
}, {
"id": 20,
"Text": "CPU DRAM",
"Children": [],
"Min": "0,8 W",
"Value": "0,9 W",
"Max": "2,1 W",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/power.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/cpu.png"
}, {
"id": 21,
"Text": "RAM",
"Children": [{
"id": 22,
"Text": "Load",
"Children": [{
"id": 23,
"Text": "Memory",
"Children": [],
"Min": "22,2 %",
"Value": "30,9 %",
"Max": "33,3 %",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/load.png"
}, {
"id": 24,
"Text": "Data",
"Children": [{
"id": 25,
"Text": "Used Memory",
"Children": [],
"Min": "1,7 GB",
"Value": "2,3 GB",
"Max": "2,5 GB",
"ImageURL": "images/transparent.png"
}, {
"id": 26,
"Text": "Available Memory",
"Children": [],
"Min": "5,0 GB",
"Value": "5,2 GB",
"Max": "5,8 GB",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/power.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/ram.png"
}, {
"id": 27,
"Text": "HDD",
"Children": [{
"id": 28,
"Text": "Temperatures",
"Children": [{
"id": 29,
"Text": "Temperature",
"Children": [],
"Min": "29,0 °C",
"Value": "30,0 °C",
"Max": "37,0 °C",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/temperature.png"
}, {
"id": 30,
"Text": "Load",
"Children": [{
"id": 31,
"Text": "Used Space",
"Children": [],
"Min": "60,6 %",
"Value": "60,6 %",
"Max": "60,6 %",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/load.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/hdd.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/computer.png"
}],
"Min": "Min",
"Value": "Value",
"Max": "Max",
"ImageURL": ""
}
标头中的脚本(来自示例):
<script>
$(document).ready(function() {
$("#retrieve-resources").click(function() {
var displayResources = $("#display-resources");
displayResources.text("Loading data from JSON source...");
$.ajax({
type: "GET",
url: "http://exampleIPhere:8085/data.json",
success: function(result) {
console.log(result);
var output =
"<table><thead><tr><th>Name</th><th>Provider</th><th>URL</th></thead><tbody>";
for (var i in result) {
output +=
"<tr><td>" +
result[i].name +
"</td><td>" +
result[i].provider +
"</td><td>" +
result[i].url +
"</td></tr>";
}
output += "</tbody></table>";
displayResources.html(output);
$("table").addClass("table");
}
});
});
});
</script>
HTML:
<div id="display-resources">
<p>jQuery example using <a href="http://api.jquery.com/jquery.ajax/">jQuery.ajax()</a> to retrieve a list of JavaScript Training Resources.</p>
</div>
<a href="javascript:void(0)" class="btn btn-primary" id="retrieve-resources">Retrieve List</a>
任何朝正确方向的推动都值得赞赏!
答案 0 :(得分:1)
我认为这个问题的棘手部分是遍历JSON。使用JSON文件时,我经常发现将示例输出粘贴到这样的站点中以使其更具可读性是很有帮助的:
在这个特定的示例中,如果每台计算机仅输出上面粘贴的JSON文件,您甚至不需要遍历结果。
console.log(obj.Children[0].Children[1].Children[0].Children[1].Text);
但是,如果有多个结果,则先前的海报正确遍历结果是正确的。我注意到您在示例代码中使用了jQuery。在该示例中,您将执行以下操作:
obj = JSONResult; // your JSON
$.each(obj, function(index, element) {
});