草庐IT

Json转Class

全部标签

ruby-on-rails - 如何在 ruby​​ in rails 中创建复杂的 Json 响应

我正在做ruby​​onrails项目,我想添加对Json的响应。一个简单的方法是:--defindex@users=User.allrespond_todo|format|format.html#index.html.erbformat.xml{render:xml=>@users}format.json{render:json=>@users.to_json}endend但是这有一些问题:-我不想在json响应中提供整个用户对象,例如密码哈希和缓存计数器属性。Facebook、twitter属性等我想在json对象中添加更多详细信息(考虑到stackoverflow模型),例如每个

ruby-on-rails - Ruby on Rails 高级 JSON 序列化

我希望在我的Rails应用程序中通过JSON呈现所有文章的索引以及完整的文章,但我在弄清楚如何去做时遇到了一些麻烦。现在这是我的Controller:ifparams[:id]@article=Article.find(params[:id])else@article=Article.published.not_draft.by_recent.firstendrespond_todo|format|format.js{render:json=>@article.to_json(:except=>[:created_at,:updated_at,:draft,:id,:publish],

ruby-on-rails - Rails JSON API 的基于 token 的身份验证

我在Rails中制作API。对于普通身份验证,我们使用设计,但在API中如何实现身份验证设计。gem'devise_token_auth'有人喜欢这个gem用于身份验证,但没有可用的教程。如何在RailsAPI中实现身份验证? 最佳答案 你能做的最好的事情就是跟随githubtutorials哪些最有可能是最新的。首先你应该遵循TLDR部分。请注意,前端开发人员需要了解usagespecification。.最后,您想阅读文档。以下是一些可能有帮助的示例:路线Rails.application.routes.drawdo#Stuff

ruby-on-rails - 将 Ruby 哈希转换为 JSON(无转义字符)

我有一个哈希:my_hash={"bob.johnson@example.com"=>{"first"=>"Bob","last"=>"Johnson"},"lisa.dell@example.com"=>{"first"=>"Lisa","last"=>"Dell"}}当我尝试用my_hash.to_json序列化它时,这是我得到的:"{\"bob.johnson@example.com\":{\"first\":\"Bob\",\"last\":\"Johnson\"},\"lisa.dell@example.com\":{\"first\":\"Lisa\",\"last\":\

ruby-on-rails - Ruby JSON.pretty_generate ... 很不漂亮

我似乎无法让JSON.pretty_generate()在Rails中实际生成漂亮的输出。我正在使用Rails2.3.5,它似乎会自动加载JSONgem。惊人的。在使用script/console时,这确实会产生JSON:some_data={'foo'=>1,'bar'=>20,'cow'=>[1,2,3,4],'moo'=>{'dog'=>'woof','cat'=>'meow'}}some_data.to_json=>"{\"cow\":[1,2,3,4],\"moo\":{\"cat\":\"meow\",\"dog\":\"woof\"},\"foo\":1,\"bar\":

ruby - 是否有类似于 Class#inherited 的钩子(Hook),仅在 Ruby 类定义后触发?

#inherited在classFoo语句之后被调用。我想要一些仅在关闭类声明的end语句之后运行的东西。这里有一些代码来举例说明我需要什么:classClassdefinheritedmputs"In#inheritedfor#{m}"endendclassFooputs"InFoo"endputs"Ireallywantedtohave#inheritedtiggeredhere."###Output:#In#inheritedforFoo#InFoo#Ireallywantedtohave#inheritedtiggeredhere.这样的东西存在吗?可以创建吗?我完全不走运吗?

ruby - 使用 ruby​​ 2.4 安装 json 1.8.3 时出错

[版本信息]ruby2.4.0p0(2016-12-24修订版57164)[x86_64-linux]/gem2.0.3/Windows10我运行了bundleinstall,它告诉我运行geminstalljson-v'1.8.3'我这样做了,但得到了一个Failedtobuildgemnativeextension错误。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingjson:ERROR:Failedtobuildgemnativeextension./home/ec2-user/.rvm/ru

ruby-on-rails - JSON 编码错误转义(Rails 3、Ruby 1.9.2)

在我的Controller中,以下工作(打印“oké”)putsobj.inspect但这不会(呈现“ok\u00e9”)render:json=>obj显然to_json方法转义了unicode字符。有没有办法阻止这种情况? 最佳答案 将\uXXXX代码设置回utf-8:json_string.gsub!(/\\u([0-9a-z]{4})/){|s|[$1.to_i(16)].pack("U")} 关于ruby-on-rails-JSON编码错误转义(Rails3、Ruby1.9.2

ruby - Class#allocate 及其用途

阅读后http://www.seejohncode.com/2012/03/16/ruby-class-allocate/并进一步研究分配方法:http://www.ruby-doc.org/core-1.9.3/Class.html#method-i-allocate我变得很好奇。Ruby的构建方式使我们不必手动为对象分配空间或释放空间,但我们可以这样做。为什么?在Ruby中手动分配对象有什么用?我看过的文章展示了一个自定义的初始化方法,但是它的用途是否如此有限? 最佳答案 allocate存在的主要原因是允许您为对象构建自定义构

ruby - 我如何使用 class_eval?

我不明白class_eval。classModuledefattr_(*syms)syms.eachdo|sym|class_eval%{def#{sym}=(val)@#{sym}=valend}endendend%是什么意思?class_eval有什么作用?(val)来自哪里? 最佳答案 简短的回答是:您可能希望避免像这样使用class_eval。这是对您的代码的解释:%{hello}只是在Ruby中编写字符串文字的另一种方式,无需担心在字符串中转义双引号或单引号:%{hello"world"}=="hello\"world\"