简体   繁体   中英

Using Ruby to Parse through JSON when the JSON object is nested?

I have a JSON object that is posted to my rails controller. For some reason Rails is unable to parse the JSON object.

This is resulting in null: Rails.logger.info params[:payload]["commits"]

Here is the rails code:

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

Any ideas why nil is being returned? Am I handling the JSON object incorrectly? Thanks

You're using the wrong path to the element you want. You need to access it as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM