如何让ruby正确编写JSON?
如何让Ruby按照JSON的结构编写 我想这样:如何让ruby正确编写JSON?,ruby,json,format,Ruby,Json,Format,如何让Ruby按照JSON的结构编写 我想这样: { "keywords": [ { "id": "1" , "product": "car" } , { "id": "2" , "product": "mobile" } ] } data_hash.store(3, 'ball') json_output = data_hash.to_json file = File.open('keywords.js
{
"keywords": [
{
"id": "1" ,
"product": "car"
} ,
{
"id": "2" ,
"product": "mobile"
}
]
}
data_hash.store(3, 'ball')
json_output = data_hash.to_json
file = File.open('keywords.json','w')
file.write(json_output)
当我用第三个对象运行代码时,
Ruby写道:
{
"keywords": [
{
"id": "1" ,
"product": "car"
} ,
{
"id": "2" ,
"product": "mobile"
}
],"3":"ball"
}
我以这种方式生成JSON:
{
"keywords": [
{
"id": "1" ,
"product": "car"
} ,
{
"id": "2" ,
"product": "mobile"
}
]
}
data_hash.store(3, 'ball')
json_output = data_hash.to_json
file = File.open('keywords.json','w')
file.write(json_output)
您可能希望使用以下内容而不是
存储
:
data_hash['keywords'] << { 'id' => '3', 'product' => 'ball' }
数据\u散列['keywords']'3','product'=>ball'}
也许你可以给我们看第三个对象
?那么如何生成JSON呢?ruby是在关键字级别之外编写的,没有关键字。我怎样才能为它编写一个模式(?)呢?您是否阅读了方法的文档?我不清楚,期望的输出。这里一切正常。你的问题是什么?不,我没读。我想要“3”:“球”在关键字级别内。这就是程序员现在的工作方式,问问题等等。不需要RTFM。如此糟糕,如此悲伤