Ruby-on-rails 将 JSON 数据嵌入到 YAML 文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9532840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Embedding JSON Data into YAML file
提问by Saurajeet
I am writing a fixture for my table. And a one of the coloums takes in a JSON string as a value.
我正在为我的桌子写一个固定装置。其中一个 coloums 接受一个 JSON 字符串作为值。
The problem is the fixture is not loading failing as:
问题是夹具没有加载失败,因为:
Fixture::FormatError: a YAML error occurred parsing /home/saurajeet/code/dcbox/test/fixtures/hardware.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html
The exact error was:
ArgumentError: syntax error on line 145, col 73: ` portslist: [{"name":"ob1","port_num":0,"port_type":"network"},{"name":"ob2","port_nu'.....
Any solutions to this.
对此的任何解决方案。
回答by Vlad Khomich
I believe taking it into quotes should do the trick:
我相信把它放在引号中应该可以解决问题:
portslist: '[{"name":"ob1","port_type" ... }]'
回答by Paul Lynch
clarkevans' comment on the accepted answer suggested a better answer for long bits of JSON, because you can wrap the lines. I looked up the block scalar syntax he mentioned, and thought I would include an example here:
clarkevans 对已接受答案的评论为长 JSON 提供了更好的答案,因为您可以将行换行。我查找了他提到的块标量语法,并认为我会在这里包含一个示例:
portslist: >
[{"name":"ob1","port_num":0,"port_type":"network"},
{"name":"ob2","port_nu...
回答by Renato Silva Das Neves
If you have the string, you can use as simple as Vlad Khomich mentioned:
如果你有字符串,你可以像 Vlad Khomich 提到的那样简单使用:
portslist: '[{"name":"ob1","port_num":0,"port_type":"network"},...]'
If you are using ERB and have an object, you can use to_json and inspect to escape to a JSON string:
如果您使用 ERB 并且有一个对象,您可以使用 to_json 和 inspect 转义为 JSON 字符串:
portslist: <%= [{name: 'ob1', port_num: 0, port_type: 'network'},...].to_json.inspect %>
And if you have a large JSON specification, you can store it in a separated file and load using Ruby, so you can keep your YAML file clean:
如果你有一个很大的 JSON 规范,你可以将它存储在一个单独的文件中并使用 Ruby 加载,这样你就可以保持你的 YAML 文件干净:
portslist: <%= File.read('/path/to/file.json').inspect %>
回答by murb
For the sake of being complete: In case you're using ActiveRecord::Store, you can load your data simply using YAML representation of the same data, even if it is a JSON store:
为了完整起见:如果您正在使用ActiveRecord::Store,您可以简单地使用相同数据的 YAML 表示加载您的数据,即使它是一个 JSON 存储:
one:
portslist:
-
name: 'ob1'
port_num: 0
port_type: 'network'
-
name: 'ob2'
port_num: 1
port_type: 'network'
回答by Norm
In my table, the column stripe_connectis of type JSONB. In the fixture, here is what worked. Note that the outer single-quotes are necessary, but square brackets are not. Everything between the single quotes is one long line.
在我的表中,列stripe_connect的类型为JSONB。在夹具中,这是有效的。请注意,外部单引号是必需的,但方括号不是。单引号之间的所有内容都是一长行。
stripe_connect: '{"scope":"read_write", "livemode":false, "token_type":"bearer", "access_token":"sk_test_madeupvalue", "refresh_token":"rt_Ae29madeupvalueyX", "stripe_user_id":"acct_103yZabcdefg", "stripe_publishable_key":"pk_test_0HEOmadeupvalue"}'

