这是什么样的数据结构?

时间:2022-09-06 17:06:27

I am pulling recent commits from github and trying to parse it using ruby. I know that I can parse it manually but I wanted to see if there was some package that could turn this into a hash or another data structure.

我正在从github上提取最近的提交,并尝试使用ruby解析它。我知道我可以手动解析它,但我想看看是否有某个包可以将它转换为散列或其他数据结构。

commits: 
- parents: 
  - id: 202fb79e8686ee127fe49497c979cfc9c9d985d5
  author:
    name: This guy
    login: tguy
    email: tguy@tguy.com
  url: a url
  id: e466354edb31f243899051e2119f4ce72bafd5f3
  committed_date: "2010-07-19T13:44:43-07:00"
  authored_date: "2010-07-19T13:33:26-07:00"
  message: |-
    message
- parents: 
  - id: c3c349ec3e9a3990cac4d256c308b18fd35d9606
  author: 
    name: Other Guy
    login: oguy
    email: oguy@gmail.com
  url: another url
  id: 202fb79e8686ee127fe49497c979cfc9c9d985d5
  committed_date: "2010-07-19T13:44:11-07:00"
  authored_date: "2010-07-19T13:44:11-07:00"
  message: this is another message

4 个解决方案

#1


5  

This is YAML http://ruby-doc.org/core/classes/YAML.html. You can do something like obj = YAML::load yaml_string (and a require 'yaml' at the top of your file, its in the standard libs), and then access it like a nested hash.

这是YAML http://ruby-doc.org/core/classes/YAML.html。您可以执行类似obj = YAML::load yaml_string(以及在文件顶部的require ' YAML ',它位于标准的libs中)的操作,然后像嵌套散列一样访问它。

YAML is basically used in the ruby world the way people use XML in the java/c# worlds.

YAML主要用于ruby世界,就像人们在java/c#世界中使用XML一样。

#2


4  

Looks like YAML to me. There are parsers for a lot of languages. For example, with the YAML library included with Ruby:

在我看来像是山猫。很多语言都有解析器。例如,使用包含Ruby的YAML库:

data = <<HERE
commits: 
- parents: 
  - id: 202fb79e8686ee127fe49497c979cfc9c9d985d5
  author:
    name: This guy
    login: tguy
    email: tguy@tguy.com
  url: a url
  id: e466354edb31f243899051e2119f4ce72bafd5f3
  committed_date: "2010-07-19T13:44:43-07:00"
  authored_date: "2010-07-19T13:33:26-07:00"
  message: |-
    message
- parents: 
  - id: c3c349ec3e9a3990cac4d256c308b18fd35d9606
  author: 
    name: Other Guy
    login: oguy
    email: oguy@gmail.com
  url: another url
  id: 202fb79e8686ee127fe49497c979cfc9c9d985d5
  committed_date: "2010-07-19T13:44:11-07:00"
  authored_date: "2010-07-19T13:44:11-07:00"
  message: this is another message
HERE

pp YAML.load data

It prints:

它打印:

{"commits"=>
  [{"author"=>{"name"=>"This guy", "login"=>"tguy", "email"=>"tguy@tguy.com"},
    "parents"=>[{"id"=>"202fb79e8686ee127fe49497c979cfc9c9d985d5"}],
    "url"=>"a url",
    "id"=>"e466354edb31f243899051e2119f4ce72bafd5f3",
    "committed_date"=>"2010-07-19T13:44:43-07:00",
    "authored_date"=>"2010-07-19T13:33:26-07:00",
    "message"=>"message"},
   {"author"=>
     {"name"=>"Other Guy", "login"=>"oguy", "email"=>"oguy@gmail.com"},
    "parents"=>[{"id"=>"c3c349ec3e9a3990cac4d256c308b18fd35d9606"}],
    "url"=>"another url",
    "id"=>"202fb79e8686ee127fe49497c979cfc9c9d985d5",
    "committed_date"=>"2010-07-19T13:44:11-07:00",
    "authored_date"=>"2010-07-19T13:44:11-07:00",
    "message"=>"this is another message"}]}

#3


2  

This format is YAML, but you can get the same information in XML or JSON, see General API Information. I'm sure there are libraries to parse those formats in Ruby.

这种格式是YAML,但是您可以在XML或JSON中获得相同的信息,请参阅通用API信息。我确信有库可以在Ruby中解析这些格式。

#4


0  

Although this isn't exactly what you're looking for, here's some more info on pulling commits. http://develop.github.com/p/commits.html. Otherwise, I think you may just need to manually parse it.

虽然这并不是你想要的,但这里有一些关于拉提交的信息。http://develop.github.com/p/commits.html。否则,我认为您可能只需要手动解析它。

#1


5  

This is YAML http://ruby-doc.org/core/classes/YAML.html. You can do something like obj = YAML::load yaml_string (and a require 'yaml' at the top of your file, its in the standard libs), and then access it like a nested hash.

这是YAML http://ruby-doc.org/core/classes/YAML.html。您可以执行类似obj = YAML::load yaml_string(以及在文件顶部的require ' YAML ',它位于标准的libs中)的操作,然后像嵌套散列一样访问它。

YAML is basically used in the ruby world the way people use XML in the java/c# worlds.

YAML主要用于ruby世界,就像人们在java/c#世界中使用XML一样。

#2


4  

Looks like YAML to me. There are parsers for a lot of languages. For example, with the YAML library included with Ruby:

在我看来像是山猫。很多语言都有解析器。例如,使用包含Ruby的YAML库:

data = <<HERE
commits: 
- parents: 
  - id: 202fb79e8686ee127fe49497c979cfc9c9d985d5
  author:
    name: This guy
    login: tguy
    email: tguy@tguy.com
  url: a url
  id: e466354edb31f243899051e2119f4ce72bafd5f3
  committed_date: "2010-07-19T13:44:43-07:00"
  authored_date: "2010-07-19T13:33:26-07:00"
  message: |-
    message
- parents: 
  - id: c3c349ec3e9a3990cac4d256c308b18fd35d9606
  author: 
    name: Other Guy
    login: oguy
    email: oguy@gmail.com
  url: another url
  id: 202fb79e8686ee127fe49497c979cfc9c9d985d5
  committed_date: "2010-07-19T13:44:11-07:00"
  authored_date: "2010-07-19T13:44:11-07:00"
  message: this is another message
HERE

pp YAML.load data

It prints:

它打印:

{"commits"=>
  [{"author"=>{"name"=>"This guy", "login"=>"tguy", "email"=>"tguy@tguy.com"},
    "parents"=>[{"id"=>"202fb79e8686ee127fe49497c979cfc9c9d985d5"}],
    "url"=>"a url",
    "id"=>"e466354edb31f243899051e2119f4ce72bafd5f3",
    "committed_date"=>"2010-07-19T13:44:43-07:00",
    "authored_date"=>"2010-07-19T13:33:26-07:00",
    "message"=>"message"},
   {"author"=>
     {"name"=>"Other Guy", "login"=>"oguy", "email"=>"oguy@gmail.com"},
    "parents"=>[{"id"=>"c3c349ec3e9a3990cac4d256c308b18fd35d9606"}],
    "url"=>"another url",
    "id"=>"202fb79e8686ee127fe49497c979cfc9c9d985d5",
    "committed_date"=>"2010-07-19T13:44:11-07:00",
    "authored_date"=>"2010-07-19T13:44:11-07:00",
    "message"=>"this is another message"}]}

#3


2  

This format is YAML, but you can get the same information in XML or JSON, see General API Information. I'm sure there are libraries to parse those formats in Ruby.

这种格式是YAML,但是您可以在XML或JSON中获得相同的信息,请参阅通用API信息。我确信有库可以在Ruby中解析这些格式。

#4


0  

Although this isn't exactly what you're looking for, here's some more info on pulling commits. http://develop.github.com/p/commits.html. Otherwise, I think you may just need to manually parse it.

虽然这并不是你想要的,但这里有一些关于拉提交的信息。http://develop.github.com/p/commits.html。否则,我认为您可能只需要手动解析它。