2016-10-25 12 views
0

Railsを初めて使用しました。JSONをRailsに挿入

私のデータベースにJSONレスポンスを挿入する必要があります。

受信応答である:

{ 
"entity_name":"Test company", 
"entity_relation":"2", 
"address1": 
{ 
"street_address":"street address for test company", 
"city":"Test city", 
"province":"Test province", 
"postal_code":"411015" 
}, 
"address2": 
{ 
"street_address":"", 
"city":"", 
"province":"", 
"postal_code":"" 
}, 
"phone_number":["1231231230"], 
"entity_website":"www.testsite.com", 
"entity_email":"[email protected]", 
"entity_description":"This is the test description", 
"controller":"frontend", 
"action":"createorganization" 
} 

アドレスは別のテーブルです。どうすれば入手できますか:

address1[street_address] 
address1[city] 
address1[province] 
address1[postal_code] 

私はデータベースに挿入できますか?

は私がやった:

data = JSON.parse data 
render json: data["address1"] 

これは、出力:あなたはちょうど通りのアドレスフィールドを取得するためにデータ[ "アドレス1"] [ "にstreet_addressを"]使用することができます

{ 
"street_address":"street address for test company", 
"city":"Test city", 
"province":"Test province", 
"postal_code":"411015" 
} 

答えて

1

0

そのすでに

{ 
      "entity_name" => "Test company", 
     "entity_relation" => "2", 
       "address1" => { 
     "street_address" => "street address for test company", 
        "city" => "Test city", 
       "province" => "Test province", 
      "postal_code" => "411015" 
    }, 
       "address2" => { 
     "street_address" => "", 
        "city" => "", 
       "province" => "", 
      "postal_code" => "" 
    }, 
      "phone_number" => [ 
     [0] "1231231230" 
    ], 
     "entity_website" => "www.testsite.com", 
      "entity_email" => "[email protected]", 
    "entity_description" => "This is the test description", 
      "controller" => "frontend", 
       "action" => "createorganization" 
} 

は今、あなたが直接アクセスすることができますが正しく

data = JSON.parse('{ 
"entity_name":"Test company", 
"entity_relation":"2", 
"address1": 
{ 
"street_address":"street address for test company", 
"city":"Test city", 
"province":"Test province", 
"postal_code":"411015" 
}, 
"address2": 
{ 
"street_address":"", 
"city":"", 
"province":"", 
"postal_code":"" 
}, 
"phone_number":["1231231230"], 
"entity_website":"www.testsite.com", 
"entity_email":"[email protected]", 
"entity_description":"This is the test description", 
"controller":"frontend", 
"action":"createorganization" 
}') 

が結果を返します

それを解析していることを確認し作業 data["address1"]

$ data["address1"] 

{ 
    "street_address" => "street address for test company", 
       "city" => "Test city", 
      "province" => "Test province", 
     "postal_code" => "411015" 
} 
関連する問題