0

因此,我们有一个 json 响应,例如: Link to Formatted Sample Json

{"C":{"1":{"1":{"A":[18],"B":[18],"C":[20],"D":[24],"E":[24],"F":[2],"G":[15],"H":[21],"I":[8]},"2":{"A":[9],"B":[26],"C":[12],"D":[10],"E":[10],"F":[3],"G":[7]},"3":{"A":[6],"B":[4],"C":[5],"D":[3],"E":[4],"F":[13]},"4":{"A":[3],"B":[2],"C":[5],"D":[13],"E":[5],"F":[5],"G":[4],"H":[7]},"5":{"A":[10],"B":[10],"C":[10],"D":[10],"E":[10],"F":[15]},"6":{"A":[10],"B":[7],"C":[5],"D":[4],"E":[7],"F":[10],"G":[4],"H":[18]},"7":{"A":[2],"B":[18],"C":[6],"D":[3],"E":[2],"F":[5],"G":[7],"H":[5],"I":[17]},"8":{"A":[20],"B":[2],"C":[10],"D":[3],"E":[5],"F":[10]},"Review 1":{"A":[30]},"Review 2":{"A":[30]}},"2":{"1":{"A":[2],"B":[3],"C":[10],"D":[10],"E":[10],"F":[15]},"10":{"A":[10],"B":[3],"C":[3],"D":[3],"E":[20]},"11":{"A":[2],"B":[6],"C":[5],"D":[10],"E":[10],"F":[13]},"2":{"A":[5],"B":[5],"C":[5],"D":[6],"E":[6],"F":[12],"G":[6],"H":[8]},"3":{"A":[3],"B":[4],"C":[8],"D":[3],"E":[2],"F":[3],"G":[12]},"4":{"A":[10],"B":[10],"C":[10],"D":[11],"E":[10],"F":[20]},"5":{"A":[8],"B":[4],"C":[8],"D":[5],"E":[14]},"6":{"A":[5],"B":[10],"C":[14],"D":[14]},"7":{"A":[3],"B":[5],"C":[8],"D":[9],"E":[10],"F":[16]},"8":{"A":[2],"B":[2],"C":[4],"D":[2],"E":[3],"F":[6],"G":[8]},"9":{"A":[2],"B":[6],"C":[5],"D":[11]},"_mex":{"1":[9]},"Review 1":{"A":[31]},"Review 2":{"A":[30]},"Review 3":{"A":[30]}},"3":{"1":{"A":[1],"B":[1],"C":[1],"D":[2],"E":[6]},"2":{"A":[2],"B":[4],"C":[7],"D":[8],"E":[8],"F":[9]},"3":{"A":[5],"B":[8],"C":[11]},"4":{"A":[10],"B":[10],"C":[11]},"5":{"A":[2],"B":[4],"C":[5],"D":[1],"E":[3],"F":[8]},"6":{"A":[4],"B":[8],"C":[8],"D":[12],"E":[8],"F":[20]},"7":{"A":[25],"B":[12],"C":[13],"D":[15],"E":[12],"F":[20]},"8":{"A":[5],"B":[3],"C":[3],"D":[7],"E":[1],"F":[1],"G":[1],"H":[1],"I":[1],"J":[3],"K":[17]},"mex2":{"A":[7]},"_mex2":{"A":[7]},"Review 1":{"A":[30]},"Review 2":{"A":[30]}},"4":{"1":{"A":[10],"B":[2],"C":[2],"D":[8],"E":[3],"F":[3]},"2":{"A":[5],"B":[10],"C":[5],"D":[10],"E":[10]},"3":{"A":[6],"B":[4],"C":[3],"D":[11]},"4":{"A":[4],"B":[4],"C":[4],"D":[4],"E":[11],"F":[21]},"5":{"A":[5],"B":[8],"C":[3],"D":[4],"E":[5],"F":[7],"G":[15],"H":[5],"I":[5],"J":[6],"K":[14]},"6":{"A":[2],"B":[4],"C":[3],"D":[2],"E":[2],"F":[3],"G":[4],"H":[4],"I":[4],"J":[4],"K":[7],"L":[34]},"_mex2":{"A":[7]},"Review 1":{"A":[77]}}}}

我想要做的是将响应中包含的所有数字相加。

我试过迭代所有的嵌套,但我只能做一个部分。使用:

@number = 0
json["C"]["1"]["1"].each do |key, val|
    val.map do |x|
        @number+=x
    end
end
#=> 150

有什么建议我会如何为 json["C"]["1"] 做同样的事情吗?

4

1 回答 1

1

基于 JSON,下面是遍历哈希的代码:

hash = JSON.parse(json)

def sum_hash(h)
  sum = 0

  h.each do |k, v|
    sum += v.is_a?(Hash) ? sum_hash(v) : v.first
  end

  sum
end

sum_hash(hash) # => 1964

必须遍历散列,并且检查每个值,因为它是不规则的。如果该值是另一个散列sum_hash,则使用该子散列调用自身,然后开始遍历接收到的子散列。

对于每个不是散列的散列值,使用从数组中检索整数first并将其添加到sum. 当方法退出时,它返回 的当前值sum,因此,一旦散列下降到,连续的sum值就会被添加。

减少 JSON 可以更轻松地确保代码执行正确的操作:

json = '
{
  "C": {
    "1": {
      "1": {
        "A": [1],
        "B": [1]
      },
      "2": {
        "A": [1],
        "B": [1]
      },
      "Review 1": {
        "A": [1]
      },
      "Review 2": {
        "A": [1]
      }
    }
  }
}
'

运行上面的代码说总和是6.

于 2013-11-13T22:26:39.410 回答