rails 3:获取一个数组,并使用正确的格式创建一个json响应

时间:2011-08-04 09:15:59

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

我需要获取一个名为@users的数据数组,并将其转换为具有正确格式的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 个答案:

答案 0 :(得分:1)

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