【问题标题】:Rails json find empty fieldRails json找到空字段
【发布时间】:2020-12-10 14:32:32
【问题描述】:

我有一些 JSON。我怎样才能得到不为空的“价值”字段?我只想得到"value": "sun-icon""value": "terracotta"

{
    "line_items": [{
        "properties": [{
                "name": "Customization Cost",
                "value": ""
            },
            {
                "name": "Standard Bundle Label 1",
                "value": "sun-icon"
            },
            {
                "name": "Custom Bundle Label 1",
                "value": ""
            },
            {
                "name": "Bundle Color 1",
                "value": "terracotta"
            }
        ]
    }]
}

【问题讨论】:

    标签: ruby-on-rails json


    【解决方案1】:

    我会这么说

    json = JSON.parse(my_str)
    
    json['line_items'].map{|hsh| hsh['properties'].select{|hsh2| hsh2['value'].empty?}}.flatten.compact
    
    

    【讨论】:

      猜你喜欢
      • 2021-08-22
      • 2018-04-12
      • 2018-12-08
      • 2019-08-20
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      相关资源
      最近更新 更多