使用Ruby将数据附加到JSON文件中 [英] Appending the data in JSON file using Ruby

查看:438
本文介绍了使用Ruby将数据附加到JSON文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ruby在JSON文件中写入/附加一些数据.在以正确格式在JSON文件中写入数据时,我遇到了问题. 我已经将值创建并存储在temphash中.我想在现有JSON文件中附加temphash的内容.我正在通过以下方式进行操作:

I am trying to write/append some data in a JSON file using ruby. I am facing issues while writing data in JSON file in proper format. I have created and stored my values in temphash. I want to append the content of temphash in existing JSON file. I am doing it in following way:

 tempHash = {"Group_Name" => @GroupName, "Group_Logo_Code" => @GroupLogoCode }

 json = File.read('public/group.json')
 secondJsonArray = JSON.parse(json)
 secondJsonHash = Hash[*secondJsonArray]

 jsonHash = []  
 jsonHash << secondJsonHash
 jsonHash << tempHash      

 File.open("public/group.json","w") do |f|
  f.puts JSON.pretty_generate(jsonHash)
 end 

这正在创建格式错误的JSON.我没有得到预期格式的JSON

This is creating malformed JSON. I am not getting the JSON in expected format

以下是我的期望:

[ 
 {
  "Group_Name": "Group Name",
  "Group_Logo_Code": "Group Logo code"
 },
 {
  "Group_Name": "Group Name",
  "Group_Logo_Code": "Group Logo code"
 },
 {
  "Group_Name": "Group Name",
  "Group_Logo_Code": "Group Logo code"
 },
]    

以下是我得到的:

[
  {
    "{\"{\\\"Group_Name\\\"=>\\\"Group Name\\\", \\\"Group_Logo_Code\\\"=>\\\"Group Logo code\\\"}\"=>{\"Group_Name\"=>\"Group Name\", \"Group_Logo_Code\"=>\"Group Logo code\"}}": {
  "Group_Name": "Group Name",
  "Group_Logo_Code": "Group Logo code"
    }
   },
  {
    "Group_Name": "Group Name",
    "Group_Logo_Code": "Group Logo code"
  }
]

请告诉我是否有更好的方法来执行此操作.谁能帮我解决这个问题.预先感谢.

Please let me know if there is any better way to do this. Can anyone please help me resolve this issue. Thanks in advance.

推荐答案

Uri Agassi的代码是完美的,但我认为解释OP代码出了什么问题.

Uri Agassi's code is perfect, but I thought to explain what went wrong in the OP code.

Hash[]需要一个key, value对的数组(作为单独的参数或数组的数组):

The Hash[] expects an array of key, value pairs (either as separate arguments or an array of arrays):

Hash[:a, 1, :b, 2]        # => {:a=>1, :b=>2}
Hash[[[:a,1], [:b,2]]]    # => {:a=>1, :b=>2}

但是原始的JSON包含哈希数组,就像简化的情况一样,该数组被解析为相应的Ruby对象:

But the original JSON contained Array of Hashes which gets parsed into corresponding Ruby objects as in the simplified case:

[{:a => 1}, {:b => 2}]

在上述数组上使用Ruby splat运算符*时:

When you use the Ruby splat operator * on the above array:

Hash[ *[{:a => 1}, {:b => 2}] ]

您可以有效地将单独的哈希作为键值对提供给Hash构造函数:

You efectively provide separate hashes as a key-value pair to the Hash constructor:

Hash[ {:a => 1}, {:b => 2} ]  # => {{:a=>1} => {:b=>2}}

因此,从本质上讲,您所需要的就是:将一个哈希数组变成一个哈希哈希,然后将其添加到一个空白数组中,并填充另一个哈希.

So, essentially, you got what you asked for: you turned a array of hashes into a hash of hashes and then you added that to a blank array and topped up with another hash.

正确的解决方案

只需使原始代码进行最少的更改即可工作(但仍请查看 Uri的解决方案):

Just to make the original code work with minimal changes (but still take a look at Uri's solution):

tempHash = {"Group_Name" => @GroupName, "Group_Logo_Code" => @GroupLogoCode }

json = File.read('public/group.json')
secondJsonArray = JSON.parse(json)

secondJsonArray << tempHash

File.open("public/group.json","w") do |f|
  f.puts JSON.pretty_generate(secondJsonArray)
end

这篇关于使用Ruby将数据附加到JSON文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆