Roson 3中的Json回应

时间:2012-02-28 01:20:47

标签: ruby-on-rails ruby ruby-on-rails-3

我有来自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"}}}]}

如何获取media_url_https字段的值?

3 个答案:

答案 0 :(得分:1)

media_url_https = myHash [“media”] [0] [“media_url_https”]

答案 1 :(得分:0)

将其分配给变量

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"}}}]}

然后

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

应该这样做。

答案 2 :(得分:0)

首先你应该将它从hash转换为json字符串, 然后在你的页面中,你应该使用javascript(jquery或ext)解码它,如果你不使用jquery或ext,你可以通过'eval'方法解码它,然后你可以通过json访问media_url_https。

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

以上代码应该可以解决您的问题!