循环遍历json数组并检索一个属性,也给出了错误

时间:2015-11-24 09:06:20

标签: ruby json

我是红宝石编程的新手,我正试图获得json['earning_rate_hr']的价值,但我收到错误,in '[]': no implicit conversion of String into Integer (TypeError)
我知道,我理解错误,但这不是我的主要问题,这是我的档案:
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

我试图做puts json,它会放置像这样的项目:

  

{“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”}

我想要做的是为每一个选择earning_rate_hr并将它们加在一起,但是我不知道如何做到这一点,因为错误没有修复,我无法得到它的值<登记/> ps:我试过把它变成一个Hash,我也尝试过使用.first,但是.first只显示第一个,我想要显示所有这些,谢谢

3 个答案:

答案 0 :(得分:1)

我从线路信使中认识你,我很久没有使用红宝石代码而且这个一直在给我cloudflare错误,我不确定它是否因为服务器停机/维护或者其他什么但是无论如何继续你的脚本,享受农业;)-LineOne

PS,我更改了一些字符串,使其看起来更清洁,因此您可以更轻松地查看垃圾邮件收入,并添加睡眠(1),因为在重新连接之前休眠一秒有助于防止云火错误 你也不需要在你的hackex脚本中需要json或rubygems,因为它在库中是必需的,所以它包含在用户前输入/脚本之前

require_relative 'libv5/lib/hackex'
while 1<2
begin
print'Filename: '
fn=gets.chomp
file = fn+'.txt'
f = File.open file, 'r'
puts "MADE BY THE PEOPLE, FOR THE PEOPLE #madebylorax" #helped by lineone
puts ""
puts "--------------------------------------------------------"
puts ""

while line = f.gets
  line = line.chomp.split(';')
  email, password = line
  HackEx.LoginDo(email, password) do |http, auth_token, user|
    puts "Retrieving Info..."
    puts''
    user = HackEx::Request.Do(http, HackEx::Request.UserInfo(auth_token))['user']
    bank = HackEx::Request.Do(http, HackEx::Request.UserBank(auth_token))['user_bank']
    json = HackEx::Request.Do(http, HackEx::Request.UserSpam(auth_token))['spam']
    cash_count=0
    tot_count=0
    json.each do |j|
      earn_rate = j['earning_rate_hr']
      total= j['total_earnings']
      cash_count+=earn_rate.to_i
      tot_count+=total.to_i
    end
    print "#{email}: current earnings: #{cash_count} per hour, Total earnings #{tot_count},"
    userchecking = bank["checking"]
    checking = userchecking.scan(/.{1,3}/).join(',')
    puts " #{checking} BTC in Checking"
    puts ""
    puts "--------------------------------------------------------"
    puts ""
    sleep 1
    end
end
rescue
puts"#{$!}"
end
end

答案 1 :(得分:1)

很好,您还可以通过在顶部示例a=0添加新变量然后在末尾添加数字a+=tot_count来计算您的农场的总收入

答案 2 :(得分:0)

这应该有所帮助:

earning_rates = json.map{|e| e["earning_rate_hr"]}
puts "Earning rates per hour: #{earning_rates.join(" ")}"
puts "Sum of earning rates: #{earning_rates.map{|e| e.to_i}.inject{|sum, x| sum + x}}"