代码之家  ›  专栏  ›  技术社区  ›  Junaid Farooq

在ruby中过滤头值不同的json

  •  -3
  • Junaid Farooq  · 技术社区  · 8 年前

    我有这种杂烩

    {"1515599671"=>
      {"Path"=>"images/1515599671.jpeg",
       "Tags"=>
        {"Anger"=>0,
         "Disgust"=>0,
         "FaceDetected"=>1,
         "Fear"=>0,
         "Happiness"=>0,
         "LargeFaceDetected"=>0,
         "MotionDetected"=>0,
         "Neutral"=>0,
         "Sadness"=>0,
         "Surprise"=>0}},
     "1525357331"=>
      {"Tags"=>
        {"Anger"=>0,
         "Disgust"=>0,
         "FaceDetected"=>0,
         "Fear"=>0,
         "Happiness"=>0,
         "LargeFaceDetected"=>0,
         "MotionDetected"=>0,
         "Neutral"=>0,
         "Sadness"=>0,
         "Surprise"=>0}},
     "1525357336"=>
      {"Tags"=>
        {"Anger"=>0,
         "Disgust"=>0,
         "FaceDetected"=>0,
         "Fear"=>0,
         "Happiness"=>0,
         "LargeFaceDetected"=>0,
         "MotionDetected"=>0,
         "Neutral"=>0,
         "Sadness"=>0,
         "Surprise"=>0}},
     "1525357341"=>
      {"Tags"=>
        {"Anger"=>0,
         "Disgust"=>0,
         "FaceDetected"=>0,
         "Fear"=>0,
         "Happiness"=>0,
         "LargeFaceDetected"=>0,
         "MotionDetected"=>0,
         "Neutral"=>0,
         "Sadness"=>0,
         "Surprise"=>0}},
     "1525357346"=>
      {"Tags"=>
        {"Anger"=>0,
         "Disgust"=>0,
         "FaceDetected"=>0,
         "Fear"=>0,
         "Happiness"=>0,
         "LargeFaceDetected"=>0,
         "MotionDetected"=>0,
         "Neutral"=>0,
         "Sadness"=>0,
         "Surprise"=>0}},
     "1525357448"=>
      {"Path"=>"images/1525357448.jpeg",
       "Tags"=>
        {"Anger"=>0,
         "Disgust"=>0,
         "FaceDetected"=>1,
         "Fear"=>0,
         "Happiness"=>0,
         "LargeFaceDetected"=>0,
         "MotionDetected"=>0,
         "Neutral"=>0,
         "Sadness"=>0,
         "Surprise"=>0}},
     "1525357449"=>
      {"Path"=>"images/1525357449.jpeg",
       "Tags"=>
        {"Anger"=>0,
         "Disgust"=>0,
         "FaceDetected"=>1,
         "Fear"=>0,
         "Happiness"=>0,
         "LargeFaceDetected"=>0,
         "MotionDetected"=>0,
         "Neutral"=>0,
         "Sadness"=>0,
         "Surprise"=>0}}}
    

    很多价值观都没有 Path 他们中很少有人有路径,我想过滤这个散列,得到一个散列,其中只有那些值,其中有路径,我已经试过了。每个和映射,但它不工作吗?任何帮助都将不胜感激谢谢

    2 回复  |  直到 8 年前
        1
  •  0
  •   Jagdeep Singh    8 年前

    你可以使用 select 关于此的哈希:

    hash = {"1515599671"=>{"Path"=>"images/1515599671.jpeg", "Tags"=>{"Anger"=>0, "Disgust"=>0, "FaceDetected"=>1, "Fear"=>0, "Happiness"=>0, "LargeFaceDetected"=>0, "MotionDetected"=>0, "Neutral"=>0, "Sadness"=>0, "Surprise"=>0}}, "1525357331"=>{"Tags"=>{"Anger"=>0, "Disgust"=>0, "FaceDetected"=>0, "Fear"=>0, "Happiness"=>0, "LargeFaceDetected"=>0, "MotionDetected"=>0, "Neutral"=>0, "Sadness"=>0, "Surprise"=>0}}, "1525357336"=>{"Tags"=>{"Anger"=>0, "Disgust"=>0, "FaceDetected"=>0, "Fear"=>0, "Happiness"=>0, "LargeFaceDetected"=>0, "MotionDetected"=>0, "Neutral"=>0, "Sadness"=>0, "Surprise"=>0}}, "1525357341"=>{"Tags"=>{"Anger"=>0, "Disgust"=>0, "FaceDetected"=>0, "Fear"=>0, "Happiness"=>0, "LargeFaceDetected"=>0, "MotionDetected"=>0, "Neutral"=>0, "Sadness"=>0, "Surprise"=>0}}, "1525357346"=>{"Tags"=>{"Anger"=>0, "Disgust"=>0, "FaceDetected"=>0, "Fear"=>0, "Happiness"=>0, "LargeFaceDetected"=>0, "MotionDetected"=>0, "Neutral"=>0, "Sadness"=>0, "Surprise"=>0}}, "1525357448"=>{"Path"=>"images/1525357448.jpeg", "Tags"=>{"Anger"=>0, "Disgust"=>0, "FaceDetected"=>1, "Fear"=>0, "Happiness"=>0, "LargeFaceDetected"=>0, "MotionDetected"=>0, "Neutral"=>0, "Sadness"=>0, "Surprise"=>0}}, "1525357449"=>{"Path"=>"images/1525357449.jpeg", "Tags"=>{"Anger"=>0, "Disgust"=>0, "FaceDetected"=>1, "Fear"=>0, "Happiness"=>0, "LargeFaceDetected"=>0, "MotionDetected"=>0, "Neutral"=>0, "Sadness"=>0, "Surprise"=>0}}}
    
    hash.select { |key, value| value['Path'] }    # Returns key-value pairs only with 'Path' set in its value. Modify the condition to remove empty strings, if you need to
     => {"1515599671"=>{"Path"=>"images/1515599671.jpeg", "Tags"=>{"Anger"=>0, "Disgust"=>0, "FaceDetected"=>1, "Fear"=>0, "Happiness"=>0, "LargeFaceDetected"=>0, "MotionDetected"=>0, "Neutral"=>0, "Sadness"=>0, "Surprise"=>0}}, "1525357448"=>{"Path"=>"images/1525357448.jpeg", "Tags"=>{"Anger"=>0, "Disgust"=>0, "FaceDetected"=>1, "Fear"=>0, "Happiness"=>0, "LargeFaceDetected"=>0, "MotionDetected"=>0, "Neutral"=>0, "Sadness"=>0, "Surprise"=>0}}, "1525357449"=>{"Path"=>"images/1525357449.jpeg", "Tags"=>{"Anger"=>0, "Disgust"=>0, "FaceDetected"=>1, "Fear"=>0, "Happiness"=>0, "LargeFaceDetected"=>0, "MotionDetected"=>0, "Neutral"=>0, "Sadness"=>0, "Surprise"=>0}}}
    
    
    # Question update
    hash.select { |key, value| value['Path'] && value['Tags'] && (value['Tags']['Anger'] == 1) }    # Select those with 'Path' set and 'Tags.Anger' with value 1
     => {}     # You don't have any such objects in your example
    
        2
  •  1
  •   Aleksei Matiushkin    8 年前

    你可以使用 iteraptor 用于此目的的宝石:

    require 'iteraptor'
    hash = { ... }
    
    keys = hash.iteraptor.select(/\APath\z/).keys
    #⇒ ["1515599671", "1525357448", "1525357449"]
    
    hash.select { |k, _| keys.include? k }