我的应用程序从HelloSign API接收带有事件数据的第三方POST回调。
问题是,我似乎无法在内部"内部" json解析它并保存数据。
class HelloSignController < ApplicationController
def receives_data
j = params[:json]
logger.info('THIS IS JSON: ' + j)
respond_to do |format|
format.json { render json: "Hello API Event Received",
status: :ok }
end
end
end
从params [:json]收到的事件数据:
{
"event": {
"event_type": "signature_request_sent",
"event_time": "1394642012",
"event_hash": "xxxxx"
},
"account_guid": null,
"client_id": "xxxx",
"signature_request": {
"signature_request_id": "xxxxxxx",
"title": "Review Agreement",
"original_title": "Review Agreement",
"message": null,
"test_mode": true,
"is_complete": false,
"final_copy_uri": "/v3/signature_request/final_copy/xxxxx",
"files_url": "https://api.hellosign.com/apiapp.php/v3/signature_request/files/xxxxx",
"details_url": "https://www.hellosign.com/home/manage?locate=xxxxx",
"requester_email_address": "xxxxxxx",
"signatures": [
{
"signature_id": "xxxxx",
"has_pin": false,
"signer_email_address": "xxxx@gmail.com",
"signer_name": "admin",
"order": null,
"status_code": "awaiting_signature",
"signed_at": null,
"last_viewed_at": null,
"last_reminded_at": null
}
]
}
}
我如何解析&#34; j&#34;保存&#34; event_type&#34;和#34; signature_request_id&#34;例如?
谢谢
另外:这是来自控制台的实际回调:
Started POST "/hs_callback" for 127.0.0.1 at 2014-03-12 13:32:38 -0400
Processing by HelloSignController#receives_data as */*
Parameters: {"json"=>"{\"event\":{\"event_type\":\"signature_request_sent\",\"event_time\":\"1394645254\",\"event_hash\":\"xxxxx\",\"event_metadata\":{\"related_signature_id\":\"\",\"reported_for_account_id\":null,\"reported_for_app_id\":\"xxxxx\",\"event_message\":null}},\"account_guid\":null,\"client_id\":\"xxxxx\",\"signature_request\":{\"signature_request_id\":\"xxxx\",\"title\":\"Review Agreement\",\"original_title\":\"Review Agreement\",\"subject\":null,\"message\":null,\"test_mode\":true,\"is_complete\":false,\"has_error\":false,\"custom_fields\":[],\"response_data\":[],\"signing_url\":null,\"signing_redirect_url\":null,\"final_copy_uri\":\"\\/v3\\/signature_request\\/final_copy\\/xxxxxx\",\"files_url\":\"https:\\/\\/api.hellosign.com\\/apiapp.php\\/v3\\/signature_request\\/files\\/xxxxxxx\",\"details_url\":\"https:\\/\\/www.hellosign.com\\/home\\/manage?locate=xxxxxx\",\"requester_email_address\":\"xxxxx@maildrop.cc\",\"signatures\":[{\"signature_id\":\"xxxxx\",\"has_pin\":false,\"signer_email_address\":\"xxxxxx@gmail.com\",\"signer_name\":\"xxxxxxx\",\"order\":null,\"status_code\":\"awaiting_signature\",\"signed_at\":null,\"last_viewed_at\":null,\"last_reminded_at\":null}],\"cc_email_addresses\":[]}}"}
答案 0 :(得分:4)
j = JSON.parse(params[:json])
j['event']['event_type'] # => "signature_request_sent"
j['signature_request']['signature_request_id'] # => "xxxxxxx"
答案 1 :(得分:2)
使用JSON.parse
。
require 'json'
#in your code
json_data = JSON.parse(params[:json])
这将解析json数据并为其返回hash
,您可以进一步阅读以获取所有变量POST
。
例如,要获取account_id,请使用json_data["client_id"]
获取signature_id
,使用json_data["signature_request"]["signatrues"][0]["signature_id"]
,依此类推。