【问题标题】:How to gather only specific data from remote JSON file如何仅从远程 JSON 文件中收集特定数据
【发布时间】:2019-06-13 06:15:34
【问题描述】:

我有一个由大约 50 台计算机组成的本地组,它们运行 Open Hardware Monitor 并将 JSON 文件输出到 http://theirip:8085/data.json 。我已将某些部分的文本分别重命名为 CPU、RAM、GPU、HDD。我正在尝试获取诸如 Value 之类的东西,它是 CPU Core 1 的子代,它是 CPU 的子代 Temperatures 的子代。但我就是想不通。

我还有一些其他代码可以加载 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>

非常感谢任何朝着正确方向的推动!

【问题讨论】:

  • 这段代码和这个 JSON 有什么关系?它用于从简单的对象数组中打印值。您的 JSON 是嵌套对象,您需要一个递归函数来遍历所有级别。然后您需要测试.text 属性是否与您要打印的内容匹配。
  • 请参阅stackoverflow.com/questions/46329392/…,了解如何在分层数据中搜索特定值。
  • 我知道,该代码是我在网上找到的一个示例,可用于从客户端机器获取 json。我正在寻求帮助的部分正是您所描述的,修改输出以遍历嵌套结果以仅获取某些 json 部分。
  • 您应该发布您编写的试图解决问题的代码,而不是您在互联网上找到的与此无关的随机代码。

标签: javascript html json


【解决方案1】:

我认为这个问题的棘手部分是遍历 JSON。在处理 JSON 文件时,我经常发现将示例输出粘贴到这样的站点中以使其更具可读性很有帮助:

http://jsoneditoronline.org/

在此特定示例中,如果每台计算机仅输出您在上面粘贴的 JSON 文件,您甚至可能不需要遍历结果。

console.log(obj.Children[0].Children[1].Children[0].Children[1].Text);

但是,如果有多个结果,之前的海报是正确的。我注意到您在示例代码中使用了 jQuery。在那个例子中你会做这样的事情:

obj = JSONResult; // your JSON
$.each(obj, function(index, element) {
});

【讨论】:

  • 好东西,这解释了很多!非常感谢!
  • 我的荣幸!很高兴它很有用!
猜你喜欢
  • 2022-11-02
  • 2017-08-08
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 2022-10-24
  • 2022-01-10
  • 2011-11-24
  • 1970-01-01
相关资源
最近更新 更多