0

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}]
4

1 に答える 1

0

PolicyListオブジェクトの配列を取得していますが、「PolicyList」というキーはありません。私はあなたがこれを必要とすると思います:

cust1 = 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"]) }

あなたのためにそれを簡単にするために。これが完全な例です。

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


require 'json'
json = <<-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}]
JSON


@@get_result = JSON.parse(json)

puts "@@get_result : #{@@get_result}\n\n"

data2 =@@get_result

cust1 = 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"]) }


puts cust1.inspect
于 2013-03-01T16:08:45.090 に答える