2011-03-02 52 views
0

我得到了一個JSON對象,我通過Rails 3(通過我的API)生成。我需要在其周圍放置一個命名的環繞標籤,或者放鬆第一個「層」。我知道這聽起來很奇怪,但我無法在客戶端循環它。解析生成的JSON的問題

這是解析代碼:

@results = RestClient.get "http://localhost:5000/emails?token=#{get_token}", :accept => :json 
@array = JSON.parse(@results); 

打開本:

[ 
    { 
     "email": { 
      "created_at": "2011-03-02T12:23:59Z", 
      "updated_at": "2011-03-02T12:23:59Z", 
      "value_1": "[email protected]", 
      "value_2": null, 
      "id": 4, 
      "value_3": null, 
      "user_id": 1, 
      "value_4": null, 
      "desc": "intevisa", 
      "privacy": null 
     } 
    }, 
    { 
     "email": { 
      "created_at": "2011-03-02T15:19:39Z", 
      "updated_at": "2011-03-02T15:19:39Z", 
      "value_1": "[email protected]", 
      "value_2": null, 
      "id": 5, 
      "value_3": null, 
      "user_id": 1, 
      "value_4": null, 
      "desc": "Some text", 
      "privacy": null 
     } 
    }, 
    { 
     "email": { 
      "created_at": "2011-03-02T15:20:17Z", 
      "updated_at": "2011-03-02T15:20:17Z", 
      "value_1": "[email protected]", 
      "value_2": null, 
      "id": 6, 
      "value_3": null, 
      "user_id": 1, 
      "value_4": null, 
      "desc": "Some text", 
      "privacy": null 
     } 
    }, 
    { 
     "email": { 
      "created_at": "2011-03-02T15:21:03Z", 
      "updated_at": "2011-03-02T15:21:03Z", 
      "value_1": "An [email protected]", 
      "value_2": null, 
      "id": 7, 
      "value_3": null, 
      "user_id": 1, 
      "value_4": null, 
      "desc": "Hello world", 
      "privacy": null 
     } 
    } 
] 

進入這個:

[ 

     "email": { 
      "created_at": "2011-03-02T12:23:59Z", 
      "updated_at": "2011-03-02T12:23:59Z", 
      "value_1": "[email protected]", 
      "value_2": null, 
      "id": 4, 
      "value_3": null, 
      "user_id": 1, 
      "value_4": null, 
      "desc": "intevisa", 
      "privacy": null 
     }, 

     "email": { 
      "created_at": "2011-03-02T15:19:39Z", 
      "updated_at": "2011-03-02T15:19:39Z", 
      "value_1": "[email protected]", 
      "value_2": null, 
      "id": 5, 
      "value_3": null, 
      "user_id": 1, 
      "value_4": null, 
      "desc": "Some text", 
      "privacy": null 
     }, 

     "email": { 
      "created_at": "2011-03-02T15:20:17Z", 
      "updated_at": "2011-03-02T15:20:17Z", 
      "value_1": "[email protected]", 
      "value_2": null, 
      "id": 6, 
      "value_3": null, 
      "user_id": 1, 
      "value_4": null, 
      "desc": "Some text", 
      "privacy": null 
     }, 

     "email": { 
      "created_at": "2011-03-02T15:21:03Z", 
      "updated_at": "2011-03-02T15:21:03Z", 
      "value_1": "An [email protected]", 
      "value_2": null, 
      "id": 7, 
      "value_3": null, 
      "user_id": 1, 
      "value_4": null, 
      "desc": "Hello world", 
      "privacy": null 
     } 

] 

這是我如何通過它在客戶端嘗試循環。

<% @array['email'].each do |item| %> 

    <%= item['value_1'] %> 

<% end %> 

回答

1

嘗試在配置>初始化> wrap_parameters.rb

# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. 
ActionController::Base.wrap_parameters format: [:json] 

# Disable root element in JSON by default. 
if defined?(ActiveRecord) 
    ActiveRecord::Base.include_root_in_json = false 
end 

下面通過設置 「include_root_in_json」 爲假,它應該讓你期望的輸出