Leon
Leon

Reputation: 1302

Json Response in RoR 3

I have this response from a json request

{"hashtags"=>[{"text"=>"mytext", "indices"=>[0, 11]}], "urls"=>[], "user_mentions"=>[], "media"=>[{"id"=>174259745790767104, "id_str"=>"174259745790767104", "indices"=>[18, 38], "media_url"=>"http://myurl.com/AmsYUMaCMAAgjlm.jpg", "media_url_https"=>"https://myurl.com/AmsYUMaCMAAgjlm.jpg", "url"=>"http://my.com/6rSti1uE", "display_url"=>"image_url", "expanded_url"=>"http://myurl.com/dir/1", "type"=>"photo", "sizes"=>{"medium"=>{"w"=>600, "h"=>450, "resize"=>"fit"}, "large"=>{"w"=>1024, "h"=>768, "resize"=>"fit"}, "small"=>{"w"=>340, "h"=>255, "resize"=>"fit"}, "thumb"=>{"w"=>150, "h"=>150, "resize"=>"crop"}, "orig"=>{"w"=>1024, "h"=>768, "resize"=>"fit"}}}]}

How can i get the value of the media_url_https field?

Upvotes: 0

Views: 119

Answers (3)

gaols
gaols

Reputation: 361

First you should convert it from hash to json string, Then in your page, you should decode it by using javascript(jquery or ext), if you don't use jquery or ext, you can decode it by 'eval' method, then you can access media_url_https via json.

var json = eval(yourResponse);
var media_url_https = json['media'][0]['media_url_https'];

The above code should solved your question!

Upvotes: 0

John H
John H

Reputation: 2488

Assign it to a variable

json = {"hashtags"=>[{"text"=>"mytext", "indices"=>[0, 11]}], "urls"=>[], "user_mentions"=>[], "media"=>[{"id"=>174259745790767104, "id_str"=>"174259745790767104", "indices"=>[18, 38], "media_url"=>"http://myurl.com/AmsYUMaCMAAgjlm.jpg", "media_url_https"=>"https://myurl.com/AmsYUMaCMAAgjlm.jpg", "url"=>"http://my.com/6rSti1uE", "display_url"=>"image_url", "expanded_url"=>"http://myurl.com/dir/1", "type"=>"photo", "sizes"=>{"medium"=>{"w"=>600, "h"=>450, "resize"=>"fit"}, "large"=>{"w"=>1024, "h"=>768, "resize"=>"fit"}, "small"=>{"w"=>340, "h"=>255, "resize"=>"fit"}, "thumb"=>{"w"=>150, "h"=>150, "resize"=>"crop"}, "orig"=>{"w"=>1024, "h"=>768, "resize"=>"fit"}}}]}

then

media_url_https = json["media"][0]["media_url_https"]

should do it.

Upvotes: 0

JimDaniel
JimDaniel

Reputation: 12713

media_url_https = myHash["media"][0]["media_url_https"]

Upvotes: 1

Related Questions