2013-03-01 5 views
0

JSON/RUBY의 새로운 기능. JSON 문자열을 반환하는 Rest Service가 있는데이를 Ruby 클래스로 구문 분석하고 싶습니다. 권장 사항입니까? 다음 예제를 시도하고 오류가 발생했습니다 - String을 Integer로 변환 할 수 없습니다. 이것은 내가 작업중인 rhomobile 앱을위한 것입니다. 나는 POJO -> JSON -> Ruby를 생각하고있다. 어떤 조언을 주시면 감사하겠습니다.루비 JSON을 루비 클래스로 매핑 - 문자열을 정수로 변환하지 마세요.

Ruby 

    @@get_result = @params['body'] 
    puts "@@get_result : #{@@get_result}" 

    data2 [email protected]@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}] 

답변

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 [email protected]@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 
+1

참고 lastPaymentDate 키도 고정되어 있습니다 (따옴표가 필요함) – rainkinz

+0

고마워요. 귀하의 제안을 실행하고 오류가 발생했습니다 : 초기화되지 않은 상수. 내 PolicyList 클래스 초기화 및지도 함수에서 새 호출하여 문제가 있습니까? –

+0

초기화되지 않은 상수는 무엇입니까? 귀하의 예제에서 적절한 코드 비트 대신이 코드를 사용하고 있습니까? – rainkinz