Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loop through json array and retrieve one attribute, gives errors also

Tags:

json

ruby

i am new to programming in ruby, and i am trying to get the value of json['earning_rate_hr'] but i get an error, in '[]': no implicit conversion of String into Integer (TypeError)
i know and i understand the error, however this is not my main question here is my file :
checkingchecker.rb :

#require_relative '../lib/hackex/net/typhoeus'
require_relative '../lib/hackex'
require 'rubygems'
require 'json'

file = 'accounts1.txt'
f = File.open file, 'r'
puts "MADE BY THE PEOPLE, FOR THE PEOPLE #madebylorax"
puts ""
puts "--------------------------------------------------------"
puts ""

while line = f.gets
  line = line.chomp.split(';')
  email, password = line
  puts "logging in as " + email
  HackEx.LoginDo(email, password) do |http, auth_token, user|
    puts "getting info..."
    user = HackEx::Request.Do(http, HackEx::Request.UserInfo(auth_token))['user']
    puts "receieved user info!"
    bank = HackEx::Request.Do(http, HackEx::Request.UserBank(auth_token))['user_bank']
    puts "recieved bank info!"
    json = HackEx::Request.Do(http, HackEx::Request.UserSpam(auth_token))['spam']
    puts "recieved spam info!"
    puts json['earning_rate_hr'] #error line, the error is because this is an array, and it cant be turned into integer, i was wondering if there is a way to use puts on it without trying to make it an integer
    userchecking = bank["checking"]
    checking = userchecking.scan(/.{1,3}/).join(',')
    puts email + " has in Checking: BTC #{checking}"
    puts ""
    puts "--------------------------------------------------------"
    puts ""
    end
end

i tried to do puts json, it puts items like this one :

{"id"=>"9867351", "user_id"=>"289108", "victim_user_id"=>"1512021", "victim_ip"= "86.60.226.175", "spam_level"=>"50", "earning_rate_hr"=>"24300", "total_earning s"=>"13267800", "started_at"=>"2015-11-01 07:46:59", "last_collected_at"=>"2015- 11-24 01:46:59"}

what i want to do is select the earning_rate_hr for each one of them and add them together, however i do not have a clue on how to do that, since the error is not fixed and i cant get the value of it
ps : i tried turning it into a Hash, and i also tried using .first, but .first only shows the firs one, i want to show all of them, thank you

like image 566
Peter Georges Bou Saada Avatar asked Mar 25 '26 09:03

Peter Georges Bou Saada


1 Answers

Thats fine you can also calculate the total income of your farms by adding new variables at the top example a=0 then adding the number at the end a+=tot_count

like image 177
Zac Burns Avatar answered Mar 28 '26 00:03

Zac Burns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!