Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中解析json并在html上创建表_Javascript_Html_Json_Parsing - Fatal编程技术网

在Javascript中解析json并在html上创建表

在Javascript中解析json并在html上创建表,javascript,html,json,parsing,Javascript,Html,Json,Parsing,我有下面的json文件,我正在阅读下面的代码 { "17.216.30.177": { "agent": { "agent_ip": "17.216.30.177", "agent_cpu_type": "Intel Core i5", "agent_serial_num": "C02KQ00CFH46", "agent_hostname": "Beautiful-iMac.l

我有下面的json文件,我正在阅读下面的代码

{
    "17.216.30.177": {
        "agent": {
            "agent_ip": "17.216.30.177",
            "agent_cpu_type": "Intel Core i5",
            "agent_serial_num": "C02KQ00CFH46",
            "agent_hostname": "Beautiful-iMac.local",
            "agent_ram": "16 GB",
            "agent_processors": "4",
            "agent_system_type": "iMac14_2"
        },
        "devices": {
            "fedaa89792fdf9bcfe819cc9981bcda918dc1fa6": {
                "SerialNumber": "",
                "HardwareModel": "abc",
                "WiFiAddress": "abc",
                "BuildVersion": "abc",
                "kanzi": "315D0F",
                "current_state": 1,
                "UniqueChipID": "612806844428"
            },
            "47d46975e929d679f1c0713f7b060bf80390aeb9": {
                "SerialNumber": "",
                "HardwareModel": "lmn",
                "WiFiAddress": "lmn",
                "BuildVersion": "lmn",
                "kanzi": "315DF3",
                "current_state": 1,
                "UniqueChipID": "2572890213651"
            }
        },
        "last_alive_time": "2016-04-27 10:24:14"
    },
    "17.153.73.241": {
        "agent": {
            "agent_hostname": "aj-air.local",
            "agent_cpu_type": "Intel Core i5",
            "agent_processors": "2",
            "agent_ip": "17.244.49.99",
            "agent_serial_num": "C02M300KFY3V",
            "agent_system_type": "MacBookAir6_2",
            "agent_ram": "8 GB"
        },
        "devices": {
            "ccd457202545bef923e2d784f0aa30e12c4dfd43": {
                "HardwareModel": "pqr",
                "kanzi": "pqr",
                "SerialNumber": "",
                "current_state": 1,
                "UniqueChipID": "pqr",
                "WiFiAddress": "pqr",
                "BuildVersion": "pqr"
            }
        },
        "last_alive_time": "2016-04-27 10:30:08"
    }
}   


    function readTextFile(file, callback) {
            var rawFile = new XMLHttpRequest();
            rawFile.overrideMimeType("application/json");
            rawFile.open("GET", file, true);
            rawFile.onreadystatechange = function() {
            if (rawFile.readyState === 4 && rawFile.status == "200") {
                callback(rawFile.responseText);
            }
        }
        rawFile.send(null);
    }


    readTextFile("/resources/test.json", function(text){
        var data = JSON.parse(text);
        document.getElementById("demo").innerHTML = data[0]["agent"].agent_ip;
    });
数据[0].17.216.30.177[“代理”]。代理ip

我怎样才能做到这一点

Ff它是
数据[0].xyz[“代理”].agent\u ip,它工作得很好,但由于ip中的周期,它不工作

我能够将json文件读入对象,但无法获取特定的键值。我从服务器获取此文件,因此不能使用带有[]的数组,如果是“;这引起了一个问题

这里的问题是,您像访问数组一样访问它,但是您没有对象数组。因此,您可以将收到的文件转换为数组,也可以将对象保持原样并更改访问它们的方式

您的主要问题是,每个人都有一个唯一的密钥,您需要引用该密钥才能访问数据。如果您提前知道它们是关键帧,则可以像使用关联数组一样使用它们。您可以通过“.”运算符访问每个属性

data["17.153.73.241"].agent.agent_ip; //Agent 2
否则,您将需要找到每个对象的密钥并使用它访问该对象

data[key].agent.agent_ip
我已经把它放在了一个关于如何在这种情况下使用键访问对象的例子中

参考资料:

  • 这对我有用

            readTextFile("/resources/test.json", function(text){
                var data = JSON.parse(text);
                for (var key in data) {
                    console.log("Agent IP-->", data[key].agent.agent_ip);
                    console.log("Number of devices -->", Object.keys(data[key].devices).length);
                    console.log("Total Number of devices -->", Object.keys(data[key].devices).length);
                }