解析并提取JSON数据

时间:2018-07-20 09:53:28

标签: ruby-on-rails json


需要下面有关JSON的指南,我正在尝试提取characterID,我应该如何处理?

预先感谢

我正在设想的JSON输出是

 "data2": {
         "characterId1": "2305843009261174417",
         "characterId2": "2305843009261174418",
         "characterId3": "2305843009283296434"

         }

JSON如下:

{
  "characters": {
    "data": {
      "2305843009261174417": {
        "membershipId": "4611686018445971568",
        "membershipType": 2,
        "characterId": "2305843009261174417"

        }

      },
      "2305843009261174418": {
        "membershipId": "4611686018445971568",
        "membershipType": 2,
        "characterId": "2305843009261174418"

        }

      },
      "2305843009283296434": {
        "membershipId": "4611686018445971568",
        "membershipType": 2,
        "characterId": "2305843009283296434"

        }


      }

}

1 个答案:

答案 0 :(得分:0)

我认为您必须首先规范化参数,因为当前是

{
  "characters": {
    "data": {
      "2305843009261174417": {
        "membershipId": "4611686018445971568",
        "membershipType": 2,
        "characterId": "2305843009261174417"
      }
    },
    "2305843009261174418": {
      "membershipId": "4611686018445971568",
      "membershipType": 2,
      "characterId": "2305843009261174418"
    }
  },
  "2305843009283296434": {
    "membershipId": "4611686018445971568",
    "membershipType": 2,
    "characterId": "2305843009283296434"
  }
}
}

并且您的预期输出是错误的,哈希中不能有相同的键

"data2": {
  "characterId": "2305843009261174417",
  "characterId": "2305843009261174418",
  "characterId": "2305843009283296434"
}

外观,data2键具有三个characterId键