Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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/5/ruby/25.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
解析、提取和处理;将JSON作为哈希返回_Json_Ruby - Fatal编程技术网

解析、提取和处理;将JSON作为哈希返回

解析、提取和处理;将JSON作为哈希返回,json,ruby,Json,Ruby,我正在尝试制作此应用的本地化版本: 我已经能够从一个本地文件中获取JSON数据&清理数字并打开JSON数据。但是,我无法提取这些值,并将它们作为已清除的哈希进行配对。这是我到目前为止所拥有的 def data_from_spreadsheet file = open(spreadsheet_url).read JSON.parse(file) end def contacts_from_spreadsheet contacts = {} data_f

我正在尝试制作此应用的本地化版本:

我已经能够从一个本地文件中获取JSON数据&清理数字并打开JSON数据。但是,我无法提取这些值,并将它们作为已清除的哈希进行配对。这是我到目前为止所拥有的

  def data_from_spreadsheet
    file = open(spreadsheet_url).read
    JSON.parse(file)
  end

  def contacts_from_spreadsheet
    contacts = {}
    data_from_spreadsheet.each do |entry|
     puts entry['name']['number']
     contacts[sanitize(number)] = name
    end
    contacts
  end
这是我正在使用的JSON数据示例

[
 {
   "name": "Michael",
   "number": 9045555555
 },
 {
   "name": "Natalie",
   "number": 7865555555
 }
]
下面是我希望JSON在contacts\u from\u电子表格方法之后的表达方式

{
  '19045555555' => 'Michael', 
  '19045555555' => 'Natalie'
}

任何帮助都将不胜感激

您似乎没有以任何方式提取
编号
名称
。我认为首先你需要更新你的代码来获得这些细节

i、 e.如果
entry
是JSON对象(或者更确切地说是在解析之前),您可以执行以下操作:

def contacts_from_spreadsheet
  contacts = {}
    data_from_spreadsheet.each do |entry|
     contacts[sanitize(entry['number'])] = entry['name']
  end
  contacts
end

您可以使用创建对数组(散列),然后调用以获取单个散列

data = [{
    "name": "Michael",
    "number": 9045555555
},
{
    "name": "Natalie",
    "number": 7865555555
}]

data.map{|e| {e[:number] => e[:name]}}.reduce Hash.new, :merge

结果:
{9045555555=>“Michael”,7865555555=>“Natalie”}

并没有将此函数保存在JSON中,但我已经解决了这个问题。这是我用过的

def data_from_spreadsheet
  file = open(spreadsheet_url).read
  YAML.load(file)
end

def contacts_from_spreadsheet
  contacts = {}
  data_from_spreadsheet.each do |entry|
    name = entry['name']
    number = entry['phone_number'].to_s
    contacts[sanitize(number)] = name
  end
  contacts
end
这将返回到此处的干净数组:

{"+19045555555"=>"Michael", "+17865555555"=>"Natalie"}

谢谢大家的加入

那么您对整个JSON部分没有问题了?那为什么要问呢?我不认为
条目['name']['number']
是您的数据结构中的一部分。您实际遇到的错误是什么?或者,
sanitize(number)
的输出是什么?这很有效!谢谢我怎样才能使数据=(file_url.json)而不是在方法中使用静态数组?
def spreadsheet_url'contacts.json'end'def contacts_from_spreadsheet sample_data=open(spreadsheet_url)sample_data.map{e{e[:number]=>e[:name]}。reduce Hash.new,:merge end
Sorry,但这还不够清楚。你能在帖子中提出问题并描述一个问题吗?