Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.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_Format - Fatal编程技术网

如何让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

如何让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.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。如此糟糕,如此悲伤