JSON/RUBYの新機能。JSON文字列を返すRESTサービスがあり、これをRubyクラスに解析したいと思います。これはお勧めですか?次の例を試しましたが、エラーが発生しました-文字列を整数に変換できません。これは私が取り組んでいるrhomobileアプリ用です。POJO->JSON->Rubyを考えています。何かアドバイスをいただければ幸いです。
Ruby
@@get_result = @params['body']
puts "@@get_result : #{@@get_result}"
data2 =@@get_result
cust1 = data2["PolicyList"].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"]) }
クラス
class PolicyList
attr_accessor :policyNbr, :systemId, :insuredName,
:type, :statusCd, :statusDes, :payorZipcode,
:lastPaymentDate,:lastPaymentAmount,:pastDueDate,
:pastDueAmount,:currentDueDate,:currentDueAmount,:eft,
:suspenseAmt,:expireTime
def initialize(policyNbr, systemId,insuredName,type,statusCd,statusDes,payorZipcode,lastPaymentDate,lastPaymentAmount,
pastDueDate,pastDueAmount,currentDueDate,currentDueAmount,eft,suspenseAmt,expireTime)
@systemId = systemId
@insuredName = insuredName
@type = type
@statusCd = statusCd
@statusDes = statusDes
@payorZipcode = payorZipcode
@lastPaymentDate = lastPaymentDate
@lastPaymentAmount = lastPaymentAmount
@pastDueDate = pastDueDate
@pastDueAmount = pastDueAmount
@currentDueDate = currentDueDate
@currentDueAmount = currentDueAmount
@eft = eft
@suspenseAmt = suspenseAmt
@expireTime = expireTime
end
end
返されたJSON
[{"policyNbr":"0000001","systemId":"MB","insuredName":"JOHN DOE ","type":"MEMBERSHIP","statusCd":"01","statusDes":"PAID","payorZipcode":"99999","lastPaymentDate":"07/12/2012","lastPaymentAmount":25.00,"pastDueDate":"","pastDueAmount":0.00,"currentDueDate":"","currentDueAmount":0.00,"eft":false,"suspenseAmt":false,"expireTime":1362152384971},{"policyNbr":"0000002","systemId":"PC","insuredName":"JOHN DOE","type":"AUTO","statusCd":"01","statusDes":"PAID","payorZipcode":"99999","lastPaymentDate":"02/15/2013","lastPaymentAmount":308.50,"pastDueDate":"","pastDueAmount":0.00,"currentDueDate":"","currentDueAmount":0.00,"eft":false,"suspenseAmt":false,"expireTime":0},{"policyNbr":"0000003","systemId":"PC","insuredName":"JOHN DOE","type":"HOME","statusCd":"01","statusDes":"PAID","payorZipcode":"99999","lastPaymentDate":"09/05/2012","lastPaymentAmount":149.00,"pastDueDate":"","pastDueAmount":0.00,"currentDueDate":"","currentDueAmount":0.00,"eft":false,"suspenseAmt":false,"expireTime":0}]