user1854802
user1854802

Reputation: 388

Parsing a JSON parameter

I must be missing something obvious - but

I have a controller. One of the action recieves the following JSON parameter

Parameters: {"user_save_name"=>{"evaluation_assumption_id"=>"51"}, "id"=>"1"}

I want to assign the value associated with the evaluation_assumption_id e.g. in this case 51

Within the controller I can get the id parameter with the statement

@jsondata = params[:id]

which gives me 1

If use the following statement within the controller

@jsondata = params[:user_save_name] 

I get {"evaluation_assumption_id"=>"51"} What I can't do is assign the value 51 to a variable. How do I do this ?
Thanks in advance Pierre

Upvotes: 1

Views: 946

Answers (1)

tihom
tihom

Reputation: 8003

params[:user_save_name] is a Hash itself so you can access the value as:

@jsondata = params[:user_save_name][:evaluation_assumption_id]
# => 51 (string)

This returns the value as string "51". If you need to convert it to an integer use to_i

@jsondata = params[:user_save_name][:evaluation_assumption_id].to_i
# => 51 (integer)

Upvotes: 4

Related Questions