Parsing json not work in variable

Hello, get the data and write it into a variable. example
openorders = Btce::TradeAPI.order_list
aaa = JSON.parse(openorders)
print aaa

Ruby200-x64/lib/ruby/gems/2.0.0/gems/json-1.8.0/lib/json/common.rb:155:in
`initialize’: no implicit conversion of Hash into String (TypeError)

This iss result 1

openorders = Btce::TradeAPI.order_list
aaa.to_s = JSON.parse(openorders)
print aaa

in <main>': undefined local variable or methodaaa’ for main:Object
(NameError)

This iss result 2

openorders = Btce::TradeAPI.order_list
print openorders

{“success”=>1, “return”=>{“29777582”=>{“pair”=>“ltc_rur”,
“type”=>“sell”, “amount”=>1.0, “rate”=>88.99999,
“timestamp_created”=>1375718423, “status”=>0},
“29777557”=>{“pair”=>“ltc_
rur”, “type”=>“sell”, “amount”=>1.0, “rate”=>89.0,
“timestamp_created”=>1375718410, “status”=>0},
“29777530”=>{“pair”=>“ltc_rur”, “type”=>“sell”, “amount”=>0.27066199,
“rate”=>90.0
, “timestamp_created”=>1375718389, “status”=>0}}}

in what could be the problem?

{“success”=>1, “return”=>{“29777582”=>{“pair”=>“ltc_rur”,
“type”=>“sell”, “amount”=>1.0, “rate”=>88.99999,
“timestamp_created”=>1375718423, “status”=>0},
“29777557”=>{“pair”=>“ltc_
rur”, “type”=>“sell”, “amount”=>1.0, “rate”=>89.0,
“timestamp_created”=>1375718410, “status”=>0},
“29777530”=>{“pair”=>“ltc_rur”, “type”=>“sell”, “amount”=>0.27066199,
“rate”=>90.0
, “timestamp_created”=>1375718389, “status”=>0}}}

What you have above is a Hash and JSON.parse() converts JSON object to
hash.
Result 1: you are trying to convert a hash to a hash using a
JSON-to-Hash parser.

Result 2: aaa is not predefined and you can’t just do aaa.to_s
what you could do is (if you want to convert the hash aaa to string)
aaa = JSON.parse(openorders)
print aaa.to_s

Hope that helps.

On 8/5/13 8:46 PM, gotostereo … wrote:

openorders = Btce::TradeAPI.order_list
aaa = JSON.parse(openorders)
print aaa

Ruby200-x64/lib/ruby/gems/2.0.0/gems/json-1.8.0/lib/json/common.rb:155:in
`initialize’: no implicit conversion of Hash into String (TypeError)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

openorders = Btce::TradeAPI.order_list

done, you already have a dictionary no parsing needed

my problem is that I do not need all that data, and only those tedious
that I want to get. Exemple need only “return”=>{“29777582” , but only
value 29777582 it really make?

h = JSON.parse(openorders)

h[ ‘return’ ].keys
#=> [“29777582”, “29777557”, “29777530”]

Big thx. All work. h[ ‘return’ ].keys This is Great

Original

{“success”=>1, “return”=>{“29777582”=>{“pair”=>“ltc_rur”,
“type”=>“sell”, “amount”=>1.0, “rate”=>88.99999,
“timestamp_created”=>1375718423, “status”=>0},
“29777557”=>{“pair”=>“ltc_
rur”, “type”=>“sell”, “amount”=>1.0, “rate”=>89.0,
“timestamp_created”=>1375718410, “status”=>0},
“29777530”=>{“pair”=>“ltc_rur”, “type”=>“sell”, “amount”=>0.27066199,
“rate”=>90.0
, “timestamp_created”=>1375718389, “status”=>0}}}

But, new problem
h[ ‘return’ ].keys Work
29777582
29777557
29777530

After analysis i realized

puts openorders.keys
success
return

Only 2 keys!

How do I get access to all the other data?
rate?
pair?

ohh…

The original structure

{
“success”:1,
“return”:{
“343152”:{
“pair”:“btc_usd”,
“type”:“sell”,
“amount”:1.00000000,
“rate”:3.00000000,
“timestamp_created”:1342448420,
“status”:0
}
}
}

On Tue, Aug 6, 2013 at 7:53 PM, gotostereo … [email protected]
wrote:

, “timestamp_created”=>1375718389, “status”=>0}}}
puts openorders.keys
success
return

Only 2 keys!

How do I get access to all the other data?
rate?
pair?

ohh…

h is a nested hash. When you do h[‘return’] you get another hash:

2.0.0p195 :006 > h[‘return’]
=> {“29777582”=>{“pair”=>“ltc_rur”, “type”=>“sell”, “amount”=>1.0,
“rate”=>88.99999, “timestamp_created”=>1375718423, “status”=>0},
“29777557”=>{“pair”=>“ltc_rur”, “type”=>“sell”, “amount”=>1.0,
“rate”=>89.0, “timestamp_created”=>1375718410, “status”=>0},
“29777530”=>{“pair”=>“ltc_rur”, “type”=>“sell”, “amount”=>0.27066199,
“rate”=>90.0, “timestamp_created”=>1375718389, “status”=>0}}

h[‘return’][‘29777582’] will return yet another hash, the one assigned
to the key 29777582:

2.0.0p195 :007 > h[‘return’][‘29777582’]
=> {“pair”=>“ltc_rur”, “type”=>“sell”, “amount”=>1.0,
“rate”=>88.99999, “timestamp_created”=>1375718423, “status”=>0}

and so on. You should read some tutorial or documentation about
hashes, in order to better understand this structure. If you want to
iterate over all the elements inside ‘return’ you can use the #each
method:

h[‘return’].each do |key, value|
puts “key #{key} has the following value assigned: #{value.inspect}”
end

Hope this helps,

Jesus.

openorders[‘return’][‘29777582’][‘pair’] This is it!
Jesus Thank you.