转换此哈希的最佳方法是什么?

时间:2015-04-08 16:14:39

标签: ruby-on-rails ruby hash

我有一个Ruby哈希,我需要转换为另一种格式。 考虑到'阵列'尺寸未知/无限制,您将如何“展平”'这个哈希到所需的格式?

原始哈希

{
  :parameters => {
    :'userResponse.objectInstanceType' => 'QuesAnsResponse',
    :'userResponse.quesAnsDetailArray' => {
      :'0' => {
        '.answer'=> 'Texas'
      },
      :'1' => {
        '.answer' => 'w3schools'
      }
    }
  }
}

所需格式:

{
  :parameters => {
    :'userResponse.objectInstanceType' => 'QuestionAnsResponse',
    :'userResponse.quesAnsDetailArray[0].answer' => 'Texas',    
    :'userResponse.quesAnsDetailArray[1].answer' => 'w3schools'
  }
}

2 个答案:

答案 0 :(得分:2)

如果您可以对输入的结构做出一些假设,即它在"userResponse.quesAnsDetailArray"部分内有编号的响应,那么您可以按照以下几点做一些事情:

new_hash = { 'parameters' => 
  { 'userResponse.objectInstanceType' => 'QuestionAnsResponse' } }

hash['parameters']['userResponse.quesAnsDetailArray'].each_pair do |index, details|
  details.each_pair do |field, value|
    new_hash['parameters']["userResponse.quesAnsDetailArray[#{index}]#{field}"] = value
  end
end

答案 1 :(得分:1)

我假设哈希如下:

h = {
  'parameters'=> {
    'userResponse.objectInstanceType'=> 'QuesAnsResponse',
    'userResponse.quesAnsDetailArray'=> {
      '0'=> {
        '.answer'=> '',
        '.answerFieldType'=> 'text',
        '.isRequired'=> 'true',
        '.metaData'=> 'QUESTION_1',
        '.questionFieldType'=> 'label',
        '.question'=> 'What is the name of your state?'
      },
      '1'=> {
        '.answer'=> '',
        '.answerFieldType'=> 'text',
        '.isRequired'=> 'true',
        '.metaData'=> 'QUESTION_2',
        '.questionFieldType'=> 'label',
        '.question'=> 'What is the name of your first school'
      }
    }
  }
}

并将其转换为所需格式,如下所示:

{ 'parameters'=> Hash[[
    ['userResponse.objectInstanceType',
      h['parameters']['userResponse.objectInstanceType']],
     *h['parameters']['userResponse.quesAnsDetailArray'].
       flat_map { |ndx,f|
         ["userResponse.quesAnsDetailArray[#{ndx}]"].product(f.to_a) }.
           map { |prefix,(suffix,value)| [prefix+suffix, value] } ]]      
}
  #=> {"parameters"=>
    {"userResponse.objectInstanceType"=>"QuesAnsResponse", 
     "userResponse.quesAnsDetailArray[0].answer"=>"", 
     "userResponse.quesAnsDetailArray[0].answerFieldType"=>"text", 
     "userResponse.quesAnsDetailArray[0].isRequired"=>"true", 
     "userResponse.quesAnsDetailArray[0].metaData"=>"QUESTION_1", 
     "userResponse.quesAnsDetailArray[0].questionFieldType"=>"label", 
     "userResponse.quesAnsDetailArray[0].question"=>
       "What is the name of your state?",
     "userResponse.quesAnsDetailArray[1].answer"=>"", 
     "userResponse.quesAnsDetailArray[1].answerFieldType"=>"text", 
     "userResponse.quesAnsDetailArray[1].isRequired"=>"true", 
     "userResponse.quesAnsDetailArray[1].metaData"=>"QUESTION_2", 
     "userResponse.quesAnsDetailArray[1].questionFieldType"=>"label", 
     "userResponse.quesAnsDetailArray[1].question"=>
       "What is the name of your first school"}}