简体   繁体   English

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

[英]Parsing json and creating table on html in Javascript

I have the following json file which in am reading with code below. 我有以下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;
    });

data[0].17.216.30.177["agent"].agent_ip ; data[0].17.216.30.177["agent"].agent_ip ;

how can I make this work? 我该如何进行这项工作?

Ff it was data[0].xyz["agent"].agent_ip; 如果是data[0].xyz["agent"].agent_ip; , it works well but because of period in ip, it doesn't ,效果很好,但是由于ip的句号,所以不

i am able to read json file into object but not able to get specific key values. 我能够将json文件读入对象,但无法获取特定的键值。 I get this file as is from server hence cant have array with [] if that';s causing aproblem. 我从服务器获取此文件,因此如果有问题,则无法使用[]数组。

The problem in this is that you are accessing it like an array, but you do not have an array of objects. 问题在于您像数组一样访问它,但是没有对象数组。 So, you can convert the file you receive into an array, or you can take the objects as they are and change the way you access them. 因此,您可以将收到的文件转换为数组,也可以按原样使用对象并更改访问它们的方式。

You main issue will be the fact that each one has a unique key that you will need to reference in order to access the data. 您面临的主要问题是每个人都有一个唯一的密钥,您需要引用该密钥才能访问数据。 If you know they keys ahead of time, you use them like an associative array. 如果您知道它们提前键,则可以像关联数组一样使用它们。 You can access each property by the '.' 您可以通过“。”访问每个属性。 operator. 操作员。

data["17.153.73.241"].agent.agent_ip; //Agent 2

Otherwise, you will need to find the key for each one and use it to access that object. 否则,您将需要为每个密钥找到密钥并使用它来访问该对象。

data[key].agent.agent_ip

I have taken put it in a jsfiddle on how to access the objects with a key for this situation. 我已经将它放在jsfiddle中,讨论了如何通过这种情况下的密钥访问对象。

References: 参考文献:

  1. Accessing elements of JSON object without knowing the key names 在不知道键名的情况下访问JSON对象的元素
  2. iterating through json object javascript 遍历JSON对象javascript
  3. Access / process (nested) objects, arrays or JSON 访问/处理(嵌套的)对象,数组或JSON
        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);
            }

this worked for me. 这对我有用。

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

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