gpt4 book ai didi

ruby-on-rails - 嵌套JSON对象时使用Ruby通过JSON解析?

转载 作者:数据小太阳 更新时间:2023-10-29 08:39:35 24 4
gpt4 key购买 nike

我有一个发布到我的 Rails Controller 的 JSON 对象。由于某些原因,Rails 无法解析 JSON 对象。

这导致空值:Rails.logger.info params[:payload]["commits"]

这是 Rails 代码:

before_filter :set_payload

def create
params[:payload] = @payload
Rails.logger.info params[:payload]["commits"]
.........

protected

def set_payload
@payload = {
"data" => {
"api_version" => "2.0.alpha1",
"username" => "jamie.winsor",
"password" => "passw0rd",
"server_url" => "http://jira"
},

"payload" => {
"after" => "a47fd41f3aa4610ea527dcc1669dfdb9c15c5425",
"ref" => "refs/heads/master",
"before" => "4c8124ffcf4039d292442eeccabdeca5af5c5017",
"compare" => "http://github.com/mojombo/grit/compare/4c8124ffcf4039d292442eeccabdeca5af5c5017...a47fd41f3aa4610ea527dcc1669dfdb9c15c5425",
"forced" => false,
"created" => false,
"deleted" => false,

"repository" => {
"name" => "grit",
"url" => "http://github.com/mojombo/grit",
"owner" => { "name" => "mojombo", "email" => "tom@mojombo.com" }
},

"pusher" => {
"name" => "rtomayko"
},

"commits" => [
{
"distinct" => true,
"removed" => [],
"message" => "[#WEB-249 status:31 resolution:1] stub git call for Grit#heads test",
"added" => [],
"timestamp" => "2007-10-10T00:11:02-07:00",
"modified" => ["lib/grit/grit.rb", "test/helper.rb", "test/test_grit.rb"],
"url" => "http://github.com/mojombo/grit/commit/06f63b43050935962f84fe54473a7c5de7977325",
"author" => { "name" => "Tom Preston-Werner", "email" => "tom@mojombo.com" },
"id" => "06f63b43050935962f84fe54473a7c5de7977325"
},
{
"distinct" => true,
"removed" => [],
"message" => "clean up heads test",
"added" => [],
"timestamp" => "2007-10-10T00:18:20-07:00",
"modified" => ["test/test_grit.rb"],
"url" => "http://github.com/mojombo/grit/commit/5057e76a11abd02e83b7d3d3171c4b68d9c88480",
"author" => { "name" => "Tom Preston-Werner", "email" => "tom@mojombo.com" },
"id" => "5057e76a11abd02e83b7d3d3171c4b68d9c88480"
},
{
"distinct" => true,
"removed" => [],
"message" => "add more comments throughout",
"added" => [],
"timestamp" => "2007-10-10T00:50:39-07:00",
"modified" => ["lib/grit.rb", "lib/grit/commit.rb", "lib/grit/grit.rb"],
"url" => "http://github.com/mojombo/grit/commit/a47fd41f3aa4610ea527dcc1669dfdb9c15c5425",
"author" => { "name" => "Tom Preston-Werner", "email" => "tom@mojombo.com" },
"id" => "a47fd41f3aa4610ea527dcc1669dfdb9c15c5425"
}
]
}
}

end

end

知道为什么返回 nil 吗?我是否错误地处理了 JSON 对象?谢谢

最佳答案

您对所需元素使用了错误的路径。您需要以以下方式访问它:

params[:payload]["payload"]["commits"]

关于ruby-on-rails - 嵌套JSON对象时使用Ruby通过JSON解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13057736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com