Ruby-将具有嵌套数组的JSON文件解析为ruby哈希,而不会丢失数据

时间:2018-12-04 11:53:55

标签: arrays json ruby

我有一个file1.json,其结构如下:

[
  {
    "uri": "features/hdp.feature",
    "id": "as-a-user-i-want-to-use-house-detailed-page",
    "keyword": "Feature",
    "name": "As a user I want to use house detailed page",
    "description": "",
    "line": 2,
    "tags": [
      {
        "name": "@hdp",
        "line": 1
      }
    ],
    "elements": [
      {

如您所见-它是一个带有嵌套键:值对和其他数组的数组。我需要将其转换为ruby hash,但是当我执行JSON.parse(file1)时-它会创建一个带有ruby哈希,数组等的数组(http://prntscr.com/lqio6r)。如果我正在执行JSON.parse(file1).reduce Hash.new, :merge or JSON.parse(file1).reduce Hash.new, :update)(作为StackOverflow的答案之一),则结果哈希将损失约60%的.json内容。能否请教我如何将json文件转换为ruby hash(不丢失任何数据)?

UPD-不被截断的数组-https://gist.githubusercontent.com/M1khah/3337507e3ca1544e6098bc726bca90cb/raw/c8262ad753bd0eebf1180e111acd016ffc07d1a5/gistfile1.txt 带有哈希的哈希-类似于这样,而不是带有嵌套哈希的数组

{
   {
        "uri": "features/hdp.feature",
        "id": "as-a-user-i-want-to-use-house-detailed-page",
        "keyword": "Feature",
        "name": "As a user I want to use house detailed page",
        "description": "",
        "line": 2,
        "tags": [
          {
            "name": "@hdp",
            "line": 1
          }
        ],
        "elements": [
          {
    }

0 个答案:

没有答案