Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 将小黄瓜解析为json_Ruby_Json_Parsing_Gherkin - Fatal编程技术网

Ruby 将小黄瓜解析为json

Ruby 将小黄瓜解析为json,ruby,json,parsing,gherkin,Ruby,Json,Parsing,Gherkin,我确信这是一个非常愚蠢的问题,但我无法理解它 我有以下ruby代码: sample_test = "Feature: Some terse yet descriptive text of what is desired Textual description of the business value of this feature Business rules that govern the scope of the feature Any additional information tha

我确信这是一个非常愚蠢的问题,但我无法理解它

我有以下ruby代码:

sample_test = "Feature: Some terse yet descriptive text of what is desired
Textual description of the business value of this feature
Business rules that govern the scope of the feature
Any additional information that will make the feature easier to understand
Scenario: Some determinable business situation
Given some precondition
  And some other precondition
 When some action by the actor
  And some other action
  And yet another action
 Then some testable outcome is achieved
  And something else we can check happens too"

io = StringIO.new
pretty_formatter    = Gherkin::Formatter::PrettyFormatter.new(io, true, false)
json_formatter      = Gherkin::Formatter::JSONFormatter.new(io)
parser = Gherkin::Parser::Parser.new(json_formatter)
result = parser.parse(sample_test, '', 0)
这将返回
True
。 但是我想得到一个JSON格式的结果。我应该使用什么来获取所有步骤的JSON输出?

好的,我找到了。效果相当不错:

require 'gherkin/parser/parser'
require 'gherkin/formatter/json_formatter'
require 'stringio'
require 'multi_json'

# This example reads a couple of features and outputs them as JSON.

io = StringIO.new
formatter = Gherkin::Formatter::JSONFormatter.new(io)
parser = Gherkin::Parser::Parser.new(formatter)

sources = ["features/native_lexer.feature", "features/escaped_pipes.feature"]
sources.each do |s|
  path = File.expand_path(File.dirname(__FILE__) + '/../' + s)
  parser.parse(IO.read(path), path, 0)
end

formatter.done
puts MultiJson.dump(MultiJson.load(io.string), :pretty => true)

你能举例说明输出应该是什么样的吗?考虑到
sample\u test
,我不清楚JSON blob应该是什么样子。没关系,我只想利用gherkin解析器做我自己的事情。我认为这是一个很好的例子,如果你展示一个你想要的输出的例子,你将有更好的机会得到答案。谢谢@Davidann我现在找到了答案,尽管在问这个问题之前谷歌搜索了那么多。