草庐IT

json - 将纪元时间戳解码为 time.Time

全部标签

ruby-on-rails - gem install json 失败,重新定义了 struct timezone/timespec

我在Windows上使用带有DevKit的Ruby1.9.3(在Win764位上都是32位)。现在我尝试安装rails,但从bundle中得到一个错误。如果我尝试运行(包在提示什么)geminstalljson我收到以下错误消息:D:\RubyTest>geminstalljsonTemporarilyenhancingPATHtoincludeDevKit...Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingjson:ERROR:Failedtobuildgemnativeextension.D:

ruby - 在 Ruby 中,如何从散列输出 json 并给它换行和制表符

我正在尝试格式化{"key"=>"value"}以将其转换为:{"key":"value"}用于写入json文件。现在我正在做:hash={"key"=>"value"}putshash.to_json.gsub('{','{\n\t')开始。这输出{\n\t"key":"value"}为什么我不能换行? 最佳答案 为漂亮的东西欢呼,为避免正则表达式欢呼!使用内置的JSON.pretty_generate方法require'json'putsJSON.pretty_generatehash,options耶!选项如下:indent:

ruby - nil.to_json 无法解析回 nil?

此代码段抛出异常:x=niljsoned=x.to_jsonputs'x.to_json='+jsoned.inspectputs'back='+JSON.parse(jsoned).inspectC:/ruby/lib/ruby/1.9.1/json/common.rb:146:in`parse':706:unexpectedtokenat'null'(JSON::ParserError)x.to_json="null"fromC:/ruby/lib/ruby/1.9.1/json/common.rb:146:in`parse'fromC:/dev/prototyping/appox

ruby-on-rails - 在 ruby​​ on rails 中使用 facebook graph api 时从 url 获取一个 json 对象

如何在我的ruby​​onrails代码中通过URL获取JSON对象:url="https://graph.facebook.com/me/friends?access_token=XXX"我无法获得有关JSON+RubyonRails的良好教程。谁能帮帮我,谢谢。 最佳答案 require'open-uri'require'json'json_object=JSON.parse(open("https://graph.facebook.com/me/friends?access_token=XXX").read)这是最简单的方法,

ruby - 在 ruby​​ 中对 JSON 哈希数组进行排序

所以我有一个哈希数组:[{"id":"30","name":"Dave"},{"id":"57","name":"Mike"},{"id":"9","name":"Kevin"},...{"id":"1","name":"Steve"}]我想按id属性对其进行排序,使其看起来像这样:[{"id":"1","name":"Steve"},{"id":"2","name":"Walter"},...{"id":"60","name":"Chester"}]我假设我使用的是sort_by方法,但我不确定该怎么做。 最佳答案 这应该有效:a

ruby-on-rails - 将时间戳添加到 db Rails 5+ 中的现有表

正在尝试将时间戳添加到现有表中。根据Apidocumenationadd_timestamps这是我的迁移代码:defchangeadd_timestamps(:products,null:false)end获取错误:*--add_timestamps(:products,{:null=>false})railsaborted!StandardError:Anerrorhasoccurred,thisandalllatermigrationscanceled:SQLite3::SQLException:CannotaddaNOTNULLcolumnwithdefaultvalueNUL

ruby-on-rails - Ruby on Rails + Sidekiq : How do I change a scheduled job start time?

我最近开始使用Sidekiq并注意到它有一个我一直在寻找的很棒的功能:UserMailer.delay_until(5.days.from_now).find_more_friends_email基本上我可以在未来安排工作,所以我不需要我的应用程序持续轮询具有开始时间的新事件。现在这很有用,但我如何更改作业的开始时间?通常一些预定的事件会更改其开始时间。我如何在sidekiq中复制它?我知道我可以删除作业并创建一个新作业,但是否可以只修改开始时间?编辑:我建立在OtoBrglez的想法之上,这里是记录的代码:moduleTaskStuffclassTaskSetterincludeSi

ruby - 如何使用 ActiveSupport 3 (Rails) 解析带有不带引号键的无效 JSON

我需要在Ruby中解析某些无效的JSON。类似于:json_str='{name:"Javier"}'ActiveSupport::JSON.decodejson_str如你所见,它是无效的,因为哈希键没有被引用,它应该是json_str='{"name":"Javier"}'但这无法更改,我必须解析未加引号的键。我可以用ActiveSupport2.x解析它,但ActiveSupport3不允许。它抛出我:Yajl::ParseError:lexicalerror:invalidstringinjsontext.{name:"Javier"}(righthere)------^顺便说

ruby-on-rails - Json -v 1.8.6 不会作为 capistrano 部署的一部分安装

这个问题已在stackoverflow和其他论坛上被问过几次,但我似乎无法通过我遇到的这个错误。当我运行capistranoproductiondeploy时,我在部署过程中收到此错误。capproductionbundler:install00:00bundler:install01bundleinstall--path/var/local/blackduck_flock_rails/shared/bundle--withoutdevelopmenttest--deployme…01Anerroroccurredwhileinstallingjson(1.8.6),andBundle

Ruby Time#to_date() 方法返回错误的日期

我们使用的是Ruby1.9.3,我发现Time#to_date似乎是一个奇怪的Ruby错误Time.new(1).to_date在应返回0001年1月1日时返回0001年1月3日。我无意中发现了这个问题。似乎如果我调用.to_datetime.to_date,结果是正确的。我还发现了一些其他相关的怪异之处。请参阅下面的irb控制台输出。(请注意,我使用的是irb,而不是railsconsole,以确保我使用的只是Ruby,不是Rails的任何附加内容。)>>require"Time"=>true>>Time.new(1).to_date=>#>>Time.new(1).to_datet