2016-10-25 4 views
0

레일스를 처음 사용했습니다.레일에 삽입 할 JSON

내 데이터베이스에 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" 
}