热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Rails使用JSON处理POST请求-RailsprocessPOSTrequestwithJSON

MyappreceivesathirdpartyPOSTcallbackfromtheHelloSignAPIwitheventdata.我的应用程序接收来自HelloS

My app receives a third party POST callback from the HelloSign API with event data.

我的应用程序接收来自HelloSign API的第三方POST回调,其中包含事件数据。

Problem is, I can't seem to get "inside" the json to parse it and save the data.

问题是,我似乎无法“进入”json来解析它并保存数据。

class HelloSignController 

The event data received from params[:json]:

来自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
        }
    ]
}
}

How can I parse "j" to save the "event_type" and the "signature_request_id" for example?

如何解析“j”来保存“event_type”和“signature_request_id”?

Thanks

谢谢

Also: this is the actual callback from the console:

另外:这是控制台的实际回调:

 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\":[]}}"}

2 个解决方案

#1


4  

j = JSON.parse(params[:json])
j['event']['event_type'] # => "signature_request_sent"
j['signature_request']['signature_request_id'] # => "xxxxxxx"

#2


2  

Use JSON.parse.

使用JSON.parse。

require 'json'

#in your code 
json_data = JSON.parse(params[:json])

This will parse the json data and return a hash for it, which you can further read to get all the variables POSTed.

这将解析json数据并返回一个散列,您可以进一步读取该散列以获得所有的变量。

For example, to get the account_id, use json_data["client_id"], to get the signature_id, use json_data["signature_request"]["signatrues"][0]["signature_id"], and so on.

例如,要获取account_id,使用json_data["client_id"]获取signature_id,使用json_data["signature_request"][" signature_request"][" signaturerues "][0]["signature_id]等。


推荐阅读
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 闭包一直是Java社区中争论不断的话题,很多语言都支持闭包这个语言特性,闭包定义了一个依赖于外部环境的自由变量的函数,这个函数能够访问外部环境的变量。本文以JavaScript的一个闭包为例,介绍了闭包的定义和特性。 ... [详细]
  • 解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法
    本文介绍了解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法,包括检查location配置是否正确、pass_proxy是否需要加“/”等。同时,还介绍了修改nginx的error.log日志级别为debug,以便查看详细日志信息。 ... [详细]
  • 网络请求模块选择——axios框架的基本使用和封装
    本文介绍了选择网络请求模块axios的原因,以及axios框架的基本使用和封装方法。包括发送并发请求的演示,全局配置的设置,创建axios实例的方法,拦截器的使用,以及如何封装和请求响应劫持等内容。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 重入锁(ReentrantLock)学习及实现原理
    本文介绍了重入锁(ReentrantLock)的学习及实现原理。在学习synchronized的基础上,重入锁提供了更多的灵活性和功能。文章详细介绍了重入锁的特性、使用方法和实现原理,并提供了类图和测试代码供读者参考。重入锁支持重入和公平与非公平两种实现方式,通过对比和分析,读者可以更好地理解和应用重入锁。 ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • Servlet多用户登录时HttpSession会话信息覆盖问题的解决方案
    本文讨论了在Servlet多用户登录时可能出现的HttpSession会话信息覆盖问题,并提供了解决方案。通过分析JSESSIONID的作用机制和编码方式,我们可以得出每个HttpSession对象都是通过客户端发送的唯一JSESSIONID来识别的,因此无需担心会话信息被覆盖的问题。需要注意的是,本文讨论的是多个客户端级别上的多用户登录,而非同一个浏览器级别上的多用户登录。 ... [详细]
  • 如何在HTML中获取鼠标的当前位置
    本文介绍了在HTML中获取鼠标当前位置的三种方法,分别是相对于屏幕的位置、相对于窗口的位置以及考虑了页面滚动因素的位置。通过这些方法可以准确获取鼠标的坐标信息。 ... [详细]
author-avatar
bbylooserxd_597
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有