Ruby,如何生成JSON格式?

时间:2016-04-22 14:13:05

标签: ruby-on-rails ruby json

我需要将以下格式的输入发送到API,我遇到了产生下面所需格式的问题。

  

{"申请" :   " {\"属性\":{\"类型\":\" genesis__Applications__c \"},\" genesis__Days_Convention__c \ ":\" ACTUAL / ACTUAL \",\" RecordTypeID \":\" 012260000004RHF \",
  \" genesis__I​​nterest_Calculation_Method__c \":\"平面\&#34 ;,
  \" genesis__I​​nterest_Rate__c \":10.0000,
  \" genesis__L​​oan_Amount__c \":22120.00,
  \" Application_Completed__c \":假,
  \" genesis__Payment_Frequency__c \":\" WEEKLY \&#34 ;,
  \" genesis__Product_Type__c \":\" LOAN \",\" genesis__Term__c \":24,   \" genesis__I​​nterest_Only_Period__c \":2,
  \" genesis__Balloon_Payment__c \":100.00}"," relatedObjects" :" {
  \" genesis__Account__c \" :{\"属性\":
  {\"输入\":\"帐户\"},\"名称\":\"伦敦金属控股   LIMITED \" },\" Loan_Product_Purpose__c \" :{\"属性\":
  {\"输入\":\" Loan_Product_Purpose__c \"},\"名称\":\"设备   购买\" }}" }

API接受上述格式作为输入。

输入规格如图所示。 enter image description here

我们尝试了JSON构建器,但没有帮助,而是生成以下格式 这对API无效。

  

" \" {:属性:{:类型:\\" genesis__Applications__c \\"},   :genesis__Days_Convention__c:\\"实际/实际\\&#34 ;,   :RecordTypeID:\\" 012260000004RHF \\&#34 ;,   :genesis__I​​nterest_Calculation_Method__c:\\"平面\\&#34 ;,   :genesis__I​​nterest_Rate__c:10.0,:genesis__L​​oan_Amount__c:22120.0,   :Application_Completed__c:假的,   :genesis__Payment_Frequency__c:\\" WEEKLY \\&#34 ;,   :genesis__Product_Type__c:\\" LOAN \\",:genesis__Term__c:24,   :genesis__I​​nterest_Only_Period__c:2,   :genesis__Balloon_Payment__c:100.0} \""

edit1:输入哈希对象

  

{:应用=> {:属性=> {:类型=>" genesis__Applications__c"},
  :genesis__Days_Convention__c =>"实际/实际&#34 ;,
  :RecordTypeID =>" 012260000004RHF&#34 ;,
  :genesis__I​​nterest_Calculation_Method__c =>"平面&#34 ;,
  :genesis__I​​nterest_Rate__c => 10.0,
  :genesis__L​​oan_Amount__c => 22120.0,
  :Application_Completed__c =>假,
  :genesis__Payment_Frequency__c =>" WEEKLY&#34 ;,
  :genesis__Product_Type__c =>" LOAN",:genesis__Term__c => 24,
  :genesis__I​​nterest_Only_Period__c =大于2,
  :genesis__Balloon_Payment _c => 100.0} ,: relatedObjects =>
  {:genesis__Account__c => {:属性=> {:类型=>"帐户"},   :名称=>"伦敦金属控股有限公司"},
  :Loan_Product_Purpose__c => {:attributes => {:type =>" Loan_Product_Purpose__c"} ,: Name =>"设备购买"}}}

1 个答案:

答案 0 :(得分:1)

你是如何使用JSON的?它应该工作得很好

require 'json'

h = {:application=> 
{:attributes=>{:type=>"genesis__Applications__c"},
             :genesis__Days_Convention__c=>"ACTUAL/ACTUAL",
             :RecordTypeID=>"012260000004RHF",
             :genesis__Interest_Calculation_Method__c=>"Flat",
             :genesis__Interest_Rate__c=>10.0,
             :genesis__Loan_Amount__c=>22120.0,
             :Application_Completed__c=>false,
             :genesis__Payment_Frequency__c=>"WEEKLY",
             :genesis__Product_Type__c=>"LOAN", :genesis__Term__c=>24,
             :genesis__Interest_Only_Period__c=>2,
             :genesis__Balloon_Payment__c=>100.0}, :relatedObjects=>
    {:genesis__Account__c=>{:attributes=>{:type=>"Account"}, :Name=>"LONDON METALS HOLDINGS LIMITED"},
:Loan_Product_Purpose__c=>{:attributes=>{:type=>"Loan_Product_Purpose__c"}, :Name=>"Equipment Purchase"}}}


puts h.to_json

打印:

 {"application":{"attributes":{"type":"genesis__Applications__c"},"genesis__Days_Convention__c":"ACTUAL/ACTUAL","RecordTypeID":"012260000004RHF","genesis__Interest_Calculation_Method__c":"Flat","genesis__Interest_Rate__c":10.0,"genesis__Loan_Amount__c":22120.0,"Application_Completed__c":false,"genesis__Payment_Frequency__c":"WEEKLY","genesis__Product_Type__c":"LOAN","genesis__Term__c":24,"genesis__Interest_Only_Period__c":2,"genesis__Balloon_Payment__c":100.0},"relatedObjects":{"genesis__Account__c":{"attributes":{"type":"Account"},"Name":"LONDON METALS HOLDINGS LIMITED"},"Loan_Product_Purpose__c":{"attributes":{"type":"Loan_Product_Purpose__c"},"Name":"Equipment Purchase"}}}

好的,现在我看到applicationrelatedObjects的值是字符串(带有json内容)。所以试试这个:

require 'json'


application = {:attributes=>{:type=>"genesis__Applications__c"},
           :genesis__Days_Convention__c=>"ACTUAL/ACTUAL",
           :RecordTypeID=>"012260000004RHF",
           :genesis__Interest_Calculation_Method__c=>"Flat",
           :genesis__Interest_Rate__c=>10.0,
           :genesis__Loan_Amount__c=>22120.0,
           :Application_Completed__c=>false,
           :genesis__Payment_Frequency__c=>"WEEKLY",
           :genesis__Product_Type__c=>"LOAN", :genesis__Term__c=>24,
           :genesis__Interest_Only_Period__c=>2,
           :genesis__Balloon_Payment__c=>100.0}

relatedObjects = {:genesis__Account__c=>{:attributes=>{:type=>"Account"}, :Name=>"LONDON METALS HOLDINGS LIMITED"},
              :Loan_Product_Purpose__c=>{:attributes=>{:type=>"Loan_Product_Purpose__c"}, :Name=>"Equipment Purchase"}}

h = {:application=> application.to_json,
     :relatedObjects=> relatedObjects.to_json}


puts h.to_json