A simple question:
In rails I get as response an hash like this:
{"success":true,"new_id":816704027}
So, the difference from a normal structure I guess is- "new_id": -instead of- new_id:
Does anyone know how to retrieve the data labelled "new_id"? The usual array["new_id"] doesn't work.
The response to the code:
new_customer_id = @response.body
puts new_customer_id
puts new_customer_id["new_id"]
is simply:
=> {"success":true,"new_id":816704028}
=> new_id
I come from the implementation of JSON_response. Anyway, they changed the app and I don't have anymore a JSON message, but they use the method:
return_200(additional_items: {:new_id => "@customer.id"} )
More:
If I write:
new_customer_id = @response.body
puts new_customer_id
puts new_customer_id[:new_id]
the answer printed is simply:
=> {"success":true,"new_id":816704028}
and the request for the :new_id content does not to be received.
Much more interesting is the following: After the fact that:
puts new_customer_id["new_id"]
prints:
=> new_id
If I write:
puts new_customer_id["new_id"][0]
puts new_customer_id["new_id"][1]
puts new_customer_id["new_id"][2]
...
I obtain:
=> n
=> e
=> w
...
Also:
if I write:
puts new_customer_id["new_"]
puts new_customer_id["new_i"]
I obtain:
=> new_
=> new_i
and if I write:
puts new_customer_id["new_id_anyOtherCharacter"]
I get nothing
Luca
That's not a ruby object you are getting back. It's JSON. You can get the new_id in a variety of ways:
JSON.parse(@response.body)["new_id"]
JSON.parse(@response.body).symbolize_keys[:new_id]
JSON.parse(@response.body).with_indifferent_access[:new_id]
使用params来获得如下值:
new_id= array[:new_id]
I bet the hash has a symbol key instead of a string key. Try with array[:new_id]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.