2013-03-04 74 views
0

我有一個從Rest中以JSON格式返回的對象數組,當我調試時我只能看到引用。我已將do循環添加到我的某個視圖中,但我沒有該對象的任何屬性。有什麼我需要做的像投擲數組?我是新來的紅寶石,並已搜索,沒有發現任何具體的。類對象的Ruby數組迭代返回查看

第一控制器不PolicyList

data3 = data2.map { |rd| PolicyList.new(rd["policyNbr"], rd["systemId"], rd["insuredName"], rd["type"], rd["statusCd"], rd["statusDes"], rd["payorZipcode"], rd["lastPaymentDate"], rd['lastPaymentAmount'], rd["pastDueDate"], rd["pastDueAmount"], rd["currentDueDate"], rd["currentDueAmount"], rd["eft"], rd["suspenseAmt"], rd["expireTime"]) } 
    $policylist =data3  
    puts data3.inspect 


    WebView.navigate(url_for(:controller => :PolicyList, :action => :index)) 

PolicyList控制器

 def index 
     #@policylists = PolicyList.find(:all) 
     @policylists = $policylist 
     render :back => '/app' 
     end 

PolicyList index.erb

<ul data-role="listview"> 
    <% @policylists.each do |policylist| %> 

     <li> 
     <a href="<%= url_for :action => :show, :id => policylist.policyNbr %>"> 
      <%= policylist.policyNbr %> 
     </a> 
     </li> 

    <% end %> 
</ul> 

PolicyList類

# The model has already been created by the framework, and extends Rhom::RhomObject 
# You can add more methods here 
class PolicyList 
     include Rhom::PropertyBag 
     attr_accessor :policyNbr, :systemId, :insuredName, 
      :type, :statusCd, :statusDes, :payorZipcode, 
      :lastPaymentDate,:lastPaymentAmount,:pastDueDate, 
      :pastDueAmount,:currentDueDate,:currentDueAmount,:eft, 
      :suspenseAmt,:expireTime 

回答

0

嘗試檢查data2變量(在映射之前)。
可能在JSON數據具有根元素,如下所示:

[ 
{"policy":{ 
    "policyNbr":"1", 
    "systemId":"2" 
    } 
}, 
{"policy":{ 
    "policyNbr":"3", 
    "systemI‌​d":"4" 
    } 
} 
] 
+0

感謝響應。我檢查並且json消息到數組的格式爲[{「policyNbr」:「1」,「systemId」:「2」},{「policyNbr」:「3」,「systemId」:「4}] data 3 = [#,#,#] – 2013-03-04 19:26:35

+0

好吧,這取決於您的類成員是如何聲明的這些字段在PolicyList類中聲明與attr_accessible? – 2013-03-04 20:34:59

+0

這應該作爲OP的更新來完成,所以其他人可以更容易地看到它。 – BlackHatSamurai 2013-03-05 00:09:01