使用jquery迭代对象并在表中解析变量

时间:2017-12-12 02:24:46

标签: jquery json loops object iteration

我尝试通过从JSON Url获得的多维对象迭代jquery。我知道如何在PHP中实现它,但在Jquery中它对我来说很安静。你能告诉我我是怎么做到的吗?

这是我的对象结构(我知道它的数组,但它因为我在PHP中将其打印为数组...对象结构是相同的,因为数据源是相同的JSON Url。

对象结构:

Array
(
    [rigs] => Array
        (
            [45334a] => Array
                (
                    [condition] => mining
                    [version] => 1.2.7
                    [driver] => amdgpu
                    [miner] => claymore-xmr
                    [gpus] => 6
                    [miner_instance] => 6
                    [miner_hashes] => 807.10 805.15 806.65 807.95 806.55 807.90
                    [bioses] => 113-C9403100_101 113-C9403100_101 113-C9403100_101 113-C9403100_101 113-C9403100_101 113-C9403100_101
                    [meminfo] => GPU0:01.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU1:02.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU2:03.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU3:04.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU4:05.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU5:06.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
                    [vramsize] => 8 8 8 8 8 8
                    [drive_name] => KINGSTON SA400S37120G 50026B76760083DD
                    [mobo] => H81 Pro BTC R2.0
                    [lan_chip] => Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 11)
                    [connected_displays] => 
                    [ram] => 7
                    [rack_loc] => devilrig
                    [ip] => 192.168.179.22
                    [server_time] => 1512871719
                    [uptime] => 1127
                    [miner_secs] => 1065
                    [rx_kbps] => 0.00
                    [tx_kbps] => 0.00
                    [load] => 0.45
                    [cpu_temp] => 33
                    [freespace] => 3.1
                    [hash] => 4841.3
                    [pool] => etn-eu1.nanopool.org:13333
                    [temp] => 74.00 73.00 74.00 74.00 74.00 52.00
                    [powertune] => 7 7 7 7 7 7
                    [voltage] => 1.150 1.150 1.150 1.150 1.150 1.150
                    [watts] => 
                    [fanrpm] => 1659 1924 1800 1659 1518 1518
                    [core] => 1380 1380 1380 1380 1380 1380
                    [mem] => 2150 2150 2150 2150 2150 2150
                )

            [469119] => Array
                (
                    [condition] => mining
                    [version] => 1.2.7
                    [driver] => amdgpu
                    [miner] => claymore
                    [gpus] => 3
                    [miner_instance] => 3
                    [miner_hashes] => 22.18 27.54 27.55
                    [bioses] => 113-C9403100_101 113-C9403100_101 113-C9403100_101
                    [meminfo] => GPU0:01.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU1:03.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
GPU2:04.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
                    [vramsize] => 8 8 8
                    [drive_name] => Hitachi HTS545050B9A302 111002PBL400P7GKD6DV
                    [mobo] => H81 Pro BTC R2.0
                    [lan_chip] => Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 11)
                    [connected_displays] => 
                    [ram] => 4
                    [rack_loc] => dragonri
                    [ip] => 192.168.179.27
                    [server_time] => 1512871598
                    [uptime] => 79751
                    [miner_secs] => 79687
                    [rx_kbps] => 0.09
                    [tx_kbps] => 0.08
                    [load] => 0.20
                    [cpu_temp] => 20
                    [freespace] => 3.1
                    [hash] => 77.27
                    [pool] => pirl.minerpool.net:8002
                    [temp] => 69.00 55.00 69.00
                    [powertune] => 3 3 3
                    [voltage] => 0.862 0.887 0.868
                    [watts] => 
                    [fanrpm] => 1518 1518 1659
                    [core] => 1075 1075 1075
                    [mem] => 2000 2000 2000
                )

            [4699d6] => Array
                (
                    [condition] => mining
                    [version] => 1.2.7
                    [driver] => fglrx
                    [miner] => claymore-xmr
                    [gpus] => 5
                    [miner_instance] => 5
                    [miner_hashes] => 831.17 832.04 801.65 825.13 814.83
                    [bioses] => 113-2E3243U-X4H 115-C671GPI-200 113-2E3243U-X4J xxx-xxx-xxx 113-2E3240U-X4A
                    [meminfo] => GPU0:01.00.0:Radeon R9 390:113-2E3243U-X4H:SK Hynix H5GC4H24AJR:GDDR5:Hawaii
GPU1:02.00.0:Radeon R9 390:115-C671GPI-200:SK Hynix H5GC4H24AJR:GDDR5:Hawaii
GPU2:03.00.0:Radeon R9 390:113-2E3243U-X4J:Elpida EDW4032BABG:GDDR5:Hawaii
GPU3:04.00.0:Radeon R9 390:xxx-xxx-xxx:SK Hynix H5GC4H24AJR:GDDR5:Hawaii
GPU4:05.00.0:Radeon R9 390:113-2E3240U-X4A:SK Hynix H5GC4H24AJR:GDDR5:Hawaii
                    [vramsize] => 8 8 8 8 8
                    [drive_name] => WDC WD10EZEX-08WN4A0 WD-WCC6Y3FS9RVS
                    [mobo] => H81 Pro BTC R2.0
                    [lan_chip] => Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 11)
                    [connected_displays] => 640x480
                    [ram] => 4
                    [rack_loc] => r9-rig
                    [ip] => 192.168.179.23
                    [server_time] => 1512871577
                    [uptime] => 83425
                    [miner_secs] => 83373
                    [rx_kbps] => 0.07
                    [tx_kbps] => 0.04
                    [load] => 0.54
                    [cpu_temp] => 40
                    [freespace] => 2.9
                    [hash] => 4104.82
                    [pool] => etn-eu1.nanopool.org:13333
                    [temp] => 72.00 60.00 61.00 73.00 54.00
                    [powertune] => 50 50 50 50 50
                    [voltage] => 0 0 0 0 0
                    [watts] => 
                    [fanrpm] => 2216 0 1924 2218 1557
                    [core] => 1040 1050 1040 1025 1010
                    [mem] => 1500 1500 1500 1500 1500
                )

            [46a259] => Array
                (
                    [condition] => stuck_miners
                    [version] => 1.2.7
                    [driver] => amdgpu
                    [miner] => claymore
                    [gpus] => 6
                    [miner_instance] => 5
                    [miner_hashes] => 24.31 28.13 24.35 00.00 24.38 24.95
                    [bioses] => 115-D000PIL-100 115-D000PIL-100 113-2E3470U.S5X 113-D00034-S07 115-D000PIL-100 113-C9403100_101
                    [meminfo] => GPU0:01.00.0:Radeon RX 480:115-D000PIL-100:Samsung K4G80325FB:GDDR5:Polaris10
GPU1:02.00.0:Radeon RX 480:115-D000PIL-100:Samsung K4G80325FB:GDDR5:Polaris10
GPU2:03.00.0:Radeon RX 480:113-2E3470U.S5X:Samsung K4G80325FB:GDDR5:Polaris10
GPU3:04.00.0:Radeon RX 570:113-D00034-S07:SK Hynix H5GC4H24AJR:GDDR5:Polaris10
GPU4:05.00.0:Radeon RX 480:115-D000PIL-100:Samsung K4G80325FB:GDDR5:Polaris10
GPU5:06.00.0:Radeon RX 580:113-C9403100_101:SK Hynix H5GQ8H24MJR:GDDR5:Polaris10
                    [vramsize] => 8 8 8 4 8 8
                    [drive_name] => KINGSTON SA400S37120G 50026B767600864A
                    [mobo] => H81 Pro BTC R2.0
                    [lan_chip] => Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 11)
                    [connected_displays] => 
                    [ram] => 4
                    [rack_loc] => smallrig
                    [ip] => 192.168.179.26
                    [server_time] => 1512871730
                    [uptime] => 51631
                    [miner_secs] => 51569
                    [rx_kbps] => 0.09
                    [tx_kbps] => 0.09
                    [load] => 0.20
                    [cpu_temp] => 42
                    [freespace] => 3.2
                    [hash] => 126.12
                    [pool] => pirl.minerpool.net:8002
                    [temp] => 64.00 64.00 74.00 35.00 64.00 56.00
                    [powertune] => 5 5 5 5 5 5
                    [voltage] => 1.143 0.956 1.031 1.131 1.150 1.150
                    [watts] => 
                    [fanrpm] => 2294 1659 988 1659 1888 1518
                    [core] => 1232 1125 1191 1168 1232 1266
                    [mem] => 2000 2050 2000 300 2000 2000
                )

        )

    [total_hash] => 9149.51
    [alive_gpus] => 19
    [total_gpus] => 20
    [alive_rigs] => 4
    [total_rigs] => 4
    [current_version] => 1.2.7
    [avg_temp] => 64.5
    [capacity] => 95.0
    [per_info] => Array
        (
            [claymore-xmr] => Array
                (
                    [hash] => 8946
                    [per_alive_gpus] => 11
                    [per_total_gpus] => 11
                    [per_alive_rigs] => 2
                    [per_total_rigs] => 2
                    [per_hash-gpu] => 813.3
                    [per_hash-rig] => 4473.0
                    [current_time] => 1512871754
                )

            [claymore] => Array
                (
                    [hash] => 203
                    [per_alive_gpus] => 8
                    [per_total_gpus] => 9
                    [per_alive_rigs] => 2
                    [per_total_rigs] => 2
                    [per_hash-gpu] => 25.4
                    [per_hash-rig] => 101.5
                    [current_time] => 1512871754
                )

        )

我如何尝试迭代它以获取变量以将其嵌入html:

function GetRigTable() {

        var rigtdata = $.getJSON('http://ellaah.ethosdistro.com/?json=yes', function (rigdata) {

            $.each($.parseJSON(rigdata.rigs), function(key,rig){
                alert(rig.rack_loc);
            });

        });

    }

我的主要问题是我需要钻机的钥匙,每一个都是不同的。 有谁可以帮助我,我们快速启动如何处理这种情况?会好的。谢谢!

2 个答案:

答案 0 :(得分:1)

如果您想访问密钥,可以使用for...in

    var rigtdata = $.getJSON('http://ellaah.ethosdistro.com/?json=yes', function (rigdata) {
        var rigs = $.parseJSON(rigdata.rigs);
        for(var key in rigs){
            alert(rigs[key].rack_loc);
            //Do whatever with key
        });

    });

答案 1 :(得分:1)

由于返回的响应已经是对象类型,因此您不必再次$.parseJSON它。为了获得迭代对象的关键,$.each回调函数的第一个参数就是您正在寻找的。

$.getJSON('http://ellaah.ethosdistro.com/?json=yes', function (rigdata) {
    $.each(rigdata.rigs, function(keyOfRig, rigValue) {
        console.log(keyOfRig, rigValue.rack_loc);
    });
});