Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
如何在Ruby中创建包含数组的JSON数据(哈希)_Ruby_Json - Fatal编程技术网

如何在Ruby中创建包含数组的JSON数据(哈希)

如何在Ruby中创建包含数组的JSON数据(哈希),ruby,json,Ruby,Json,我试图找到一种在ruby中对JSON数据进行排序的方法。它存储为一个相当复杂的散列(我想无论如何也是如此),如下所示: { "allergies": { "allergy": [ { "id": "11426793", "name": "Milk", "category": "Food Allergy", "created

我试图找到一种在ruby中对JSON数据进行排序的方法。它存储为一个相当复杂的散列(我想无论如何也是如此),如下所示:

{
    "allergies": {
        "allergy": [
            {
                "id": "11426793",
                "name": "Milk",
                "category": "Food Allergy",
                "createdPerson": "AGUDELO-HERNANDEZ  ARCADIO",
                "onsetDate": "2014-05-05T00:29:28-04:00"
            }, {
                "id": "11426788",
                "name": "Antibiotics",
                "category": "Drug Allergy",
                "createdPerson": "Smith John H",
                "onsetDate": "2014-05-04T22:29:28-04:00"
            }
        ]
    },
    "responseErrors": {
        "responseError": []
    }
}
实际数据中存在更多的“过敏”对象,我希望能够按“onsetDate”然后按“name”对它们进行排序。我试过:

sorted = @allergies["allergies"].sort_by { |hsh| hsh["name"] }
但我得到了以下错误:

no implicit conversion of String into Integer (TypeError)
你就快到了:

require "json"

str = <<EOS 
 {
  "allergies": {
    "allergy": [
    {
      "id": "11426793",
      "name": "Milk",
      "category": "Food Allergy",
      "createdPerson": "AGUDELO-HERNANDEZ  ARCADIO",
      "onsetDate": "2014-05-05T00:29:28-04:00"
   },
   {
     "id": "11426788",
     "name": "Antibiotics",
     "category": "Drug Allergy",
     "createdPerson": "Smith John H",
     "onsetDate": "2014-05-04T22:29:28-04:00"
   }

]
},
"responseErrors": {
  "responseError": []
}
}
EOS

@allergies = JSON.parse(str)
puts @allergies["allergies"]["allergy"].sort_by { |hsh| hsh["name"] }
需要“json”
str=
@allergies[“allergies”]
返回一个散列,即:

{
    "allergy": [
        {
            "id": "11426793",
            "name": "Milk",
            "category": "Food Allergy",
            "createdPerson": "AGUDELO-HERNANDEZ  ARCADIO",
            "onsetDate": "2014-05-05T00:29:28-04:00"
        }, {
            "id": "11426788",
            "name": "Antibiotics",
            "category": "Drug Allergy",
            "createdPerson": "Smith John H",
            "onsetDate": "2014-05-04T22:29:28-04:00"
        }
    ]
}
对散列调用
sort\u by
,它将为块生成每个键值对。所以在

@allergies["allergies"].sort_by { |hsh| hsh["name"] }
hsh
是一个数组
Array#[]
需要整数作为参数。它将尝试将
“name”
转换为整数,并失败,出现您看到的错误

你需要

@allergies["allergies"]["allergy"].sort_by { |hsh| hsh["name"] }
或者,如果要对值进行适当排序,以便原始数据已排序,请使用排序依据