0

Im try to iterate with jquery through multidimensional Object i got from a JSON Url. I know how to do it in PHP but in Jquery its quiet new to me. Can you tell me how i do it the right way ?

This here is my Object Structure (I know its array but its because i printed it as array in PHP...the object structure is the same since the data source is the same JSON Url.

OBJECT Structure:

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
                )

        )

How i tried to iterate through it to get the variables to embed it in 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);
            });

        });

    }

My mainproblem is that i need the keys of rigs and each one is different. could anyone maybe help me we a quickstart how to handle such a situation ? Would be quit nice. Thanks!

2 Answers 2

1

Since returned response is already an object type, you don't have to $.parseJSON it again. And to get the key of the iterating object, first parameter of $.each callback function is what you're looking for.

$.getJSON('http://ellaah.ethosdistro.com/?json=yes', function (rigdata) {
    $.each(rigdata.rigs, function(keyOfRig, rigValue) {
        console.log(keyOfRig, rigValue.rack_loc);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can use for...in if you want to access the keys

    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
        });

    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.