2011-08-04 2 views
0

@users이라는 데이터 배열을 가져 와서 올바른 형식의 json 응답으로 변환해야합니다.레일 3 : 배열을 가져 와서 올바른 형식으로 json 응답을 작성하십시오.

@users 배열에는 많은 특성이 있습니다. 난 단지 idscreen_name 필요하지만, 나는 다음과 같이보고 서식을 필요

[ 
    {"id":"856","screen_name":"joerogan"}, 
    {"id":"1035","screen_name":"redban"}, 
    {"id":"1245","screen_name":"madflavor"} 
    ... 
] 

def user_json_list 
... 

results = @users # ? 

respond_to do |format| 
     format.html 
     format.json { render :json => results } 
end 
end 

@users 배열은 다음과 같습니다

[{ 
    "profile_sidebar_fill_color":"C0DFEC", 
    "protected":false, 
    "is_translator":false, 
    "default_profile":false, 
    "lang":"en", 
    "profile_background_tile":false, 
    "profile_image_url":"http://a3.twimg.com/profile_images/1443034111/new_avatar_normal.jpg", 
    "name":"Joe Rogan", 
    "follow_request_sent":false, 
    "verified":true, 
    "utc_offset":-25200, 
    "profile_link_color":"0084B4", 
    "followers_count":416343, 
    "screen_name":"joerogan", 
    "profile_sidebar_border_color":"a8c7f7", 
    "location":"","contributors_enabled":false, 
    "time_zone":"Mountain Time (US & Canada)", 
    "status": 
    { 
     "favorited":false, 
     "in_reply_to_status_id_str":null, 
     "place":null, 
     "in_reply_to_screen_name":null, 
     "in_reply_to_user_id_str":null, 
     "contributors":null,"geo":null, 
     "retweeted":false, 
     "truncated":false, 
     "coordinates":null, 
     "text":"The Joe Rogan Daily is out! http://bit.ly/bgRzbI \u25b8 Top stories today via @bryanbrinkman @madmanwoo", 
     "in_reply_to_user_id":null, 
     "retweet_count":0, 
     "in_reply_to_status_id":null, 
     "id_str":"99032523769782272", 
     "source":"<a href=\"http://paper.li\" rel=\"nofollow\">Paper.li</a>", 
     "created_at":"Thu Aug 04 08:22:48 +0000 2011", 
     "possibly_sensitive":false, 
     "id":99032523769782272 
    }, 
    "show_all_inline_media":false, 
    "listed_count":9675, 
    "following":true, 
    "geo_enabled":false, 
    "profile_use_background_image":true, 
    "profile_background_image_url_https":"https://si0.twimg.com/profile_background_images/293713769/back.jpg", 
    "url":"http://www.joerogan.net", 
    "description":"Stand up comic/mixed martial arts fanatic/psychedelic adventurer/occasional tv whore\r\n\r\nSayNow number: 818-237-4245", 
    "profile_background_color":"022330", 
    "profile_image_url_https":"https://si0.twimg.com/profile_images/1443034111/new_avatar_normal.jpg", 
    "id_str":"18208354", 
    "profile_background_image_url":"http://a0.twimg.com/profile_background_images/293713769/back.jpg", 
    "created_at":"Thu Dec 18 04:01:47 +0000 2008", 
    "statuses_count":7464, 
    "notifications":false, 
    "friends_count":982,"id":18208354, 
    "default_profile_image":false, 
    "favourites_count":88,"profile_text_color":"333333" 
}, 

{ 
    "listed_count":517, 
    "profile_background_image_url_https":"https://si0.twimg.com/profile_background_images/188153775/redbak.jpg", 
    "profile_sidebar_fill_color":"C0DFEC", 
    "protected":false, 
    "lang":"en", 
    "profile_background_tile":true, 
    "profile_image_url_https":"https://si0.twimg.com/profile_images/1475168425/189391_2151954270770_1003770965_32544227_8147843_n_normal.jpg", 
    "profile_image_url":"http://a3.twimg.com/profile_images/1475168425/189391_2151954270770_1003770965_32544227_8147843_n_normal.jpg", 
    "name":"redban", 
    "default_profile":false, 
    "verified":false, 
    "utc_offset":-28800, 
    "friends_count":1074, 
    "profile_link_color":"0084B4", 
    "followers_count":26651, 
    "screen_name":"redban", 
    "default_profile_image":false, 
    "profile_sidebar_border_color":"a8c7f7", 
    "location":"Burbank, CA", 
    "time_zone":"Pacific Time (US & Canada)", 
    "status": 
    { 
     "favorited":false, 
     "in_reply_to_status_id_str":"98986315491713024", 
     "place":null,"in_reply_to_screen_name":"WarriorPoetUS", 
     "in_reply_to_user_id_str":"105196298", 
     "contributors":null, 
     "geo":null, 
     "retweeted":false, 
     "truncated":false, 
     "coordinates":null, 
     "text":"@WarriorPoetUS I feel blessed just knowing you brotha!", 
     "in_reply_to_user_id":105196298, 
     "retweet_count":0, 
     "in_reply_to_status_id":98986315491713024, 
     "id_str":"99002282691137536", 
     "source":"<a href=\"http://twitter.com/#!/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>", 
     "created_at":"Thu Aug 04 06:22:38 +0000 2011", 
     "id":99002282691137536 
    }, 
    "is_translator":false, 
    "following":false, 
    "profile_use_background_image":true, 
    "url":"http://www.redban.com", 
    "description":"If you know me, you're paying attention. For all the podcasts I produce check out: http://www.deathsquad.tv and subscribe to DEATHSQUAD on iTunes.", 
    "show_all_inline_media":true, 
    "follow_request_sent":false, 
    "geo_enabled":false, 
    "profile_background_color":"022330", 
    "id_str":"14829533", 
    "profile_background_image_url":"http://a0.twimg.com/profile_background_images/188153775/redbak.jpg", 
    "created_at":"Mon May 19 06:48:27 +0000 2008", 
    "notifications":false, 
    "id":14829533, 
    "contributors_enabled":false, 
    "statuses_count":5753, 
    "favourites_count":71, 
    "profile_text_color":"333333" 
}, 

{ 
    "show_all_inline_media":false, 
    "geo_enabled":false, 
    "profile_sidebar_fill_color":"DDEEF6", 
    "protected":false, 
    "profile_image_url_https":"https://si0.twimg.com/profile_images/295809307/doc_normal.jpg", 
    "lang":"en", 
    "profile_background_tile":false, 
    "profile_image_url":"http://a1.twimg.com/profile_images/295809307/doc_normal.jpg", 
    "name":"Joey CoCo Diaz", 
    "verified":false, 
    "friends_count":99, 
    "default_profile_image":false, 
    "statuses_count":3440, 
    "utc_offset":-28800, 
    "profile_link_color":"0084B4", 
    "followers_count":26489, 
    "screen_name":"madflavor", 
    "profile_sidebar_border_color":"C0DEED", 
    "location":"Los Angeles CALIFORNIA", 
    "is_translator":false, 
    "time_zone":"Pacific Time (US & Canada)", 
    "status": 
    { 
     "favorited":false, 
     "in_reply_to_status_id_str":"99014475436408833", 
     "place":null, 
     "in_reply_to_screen_name":"msunner1", 
     "in_reply_to_user_id_str":"308440624", 
     "contributors":null, 
     "geo":null, 
     "retweeted":false, 
     "truncated":false, 
     "coordinates":null, 
     "text":"@msunner1 Real soon! Much love!", 
     "in_reply_to_user_id":308440624, 
     "retweet_count":0, 
     "in_reply_to_status_id":99014475436408833, 
     "id_str":"99029735958511616", 
     "source":"web", 
     "created_at":"Thu Aug 04 08:11:43 +0000 2011", 
     "id":99029735958511616 
    }, 
    "follow_request_sent":false, 
    "following":false, 
    "profile_use_background_image":true, 
    "url":"http://blogs.myspace.com/39708886", 
    "description":"LIFECOACH/COMEDIAN/ CRIMINAL/ WEIGHTWATCHER", 
    "profile_background_color":"C0DEED", 
    "id_str":"29226377", 
    "profile_background_image_url":"http://a0.twimg.com/images/themes/theme1/bg.png", 
    "created_at":"Mon Apr 06 15:49:20 +0000 2009", 
    "contributors_enabled":false, 
    "notifications":false, 
    "id":29226377, 
    "default_profile":true, 
    "listed_count":547, 
    "profile_background_image_url_https":"https://si0.twimg.com/images/themes/theme1/bg.png", 
    "favourites_count":3, 
    "profile_text_color":"333333" 
}] 

답변

1
respond_to do |format| 
    format.html 
    format.json { render :json => @users.map{|u| {:id => u.id, :screen_name => u.screen_name}} } 
end 
+0

덕분에, 더 나은 것 전에이 작업을 수행 할 수 respond_to 또는 {render : json => ...}에서 동일한 작업을 수행합니까? –

+0

@ users.map의 결과를 respond_to보다 먼저 로컬 변수에 할당 할 수 있습니다. 그러나 html 형식을 렌더링 할 때 필요하지 않은 경우 건너 뛸 수 있으며 format.json 내부에서만 사용할 수 있습니다. – Hck

관련 문제