Как извлечь данные из JSON в Ruby on Rails - PullRequest
0 голосов
/ 21 ноября 2019

У меня есть ответ JSON от httparty:

{
    "banks": [{
        "sync_frequency": null,
        "code": null,
        "uuid": "2ac14818-59fb-50cf-b3e4-3bddcdfefcae",
        "documents_type": [],
        "color": "003d8f",
        "id": 479,
        "available_transfer_mechanisms": [],
        "auth_mechanism": "credentials",
        "capabilities": ["document"],
        "transfer_beneficiary_types": [],
        "available_auth_mechanisms": ["credentials"],
        "beta": true,
        "months_to_fetch": null,
        "urls": ["https://login.ionos.fr", "https://my.ionos.fr"],
        "siret": null,
        "hidden": false,
        "charged": true,
        "slug": "1&1",
        "categories": [],
        "name": "1&1"
    }, {
        "sync_frequency": null,
        "code": null,
        "uuid": "fabe9547-ba74-5f8c-a790-f5ecc7c4626b",
        "documents_type": [],
        "color": "ffa500",
        "id": 492,
        "available_transfer_mechanisms": [],
        "auth_mechanism": "credentials",
        "capabilities": ["document"],
        "transfer_beneficiary_types": [],
        "available_auth_mechanisms": ["credentials"],
        "beta": true,
        "months_to_fetch": null,
        "urls": ["http://www.avantages-entreprises.com"],
        "siret": null,
        "hidden": false,
        "charged": true,
        "slug": "ACE",
        "categories": [],
        "name": "aceos"
    }, {
        "sync_frequency": null,
        "code": null,
        "uuid": "5f778b18-d10e-5ffd-98a9-ed61d1d39f9b",
        "account_types": ["perp", "madelin", "lifeinsurance"],
        "documents_type": [],
        "color": "005090",
        "id": 406,
        "available_transfer_mechanisms": [],
        "auth_mechanism": "credentials",
        "capabilities": ["bankwealth", "document", "bank"],
        "transfer_beneficiary_types": [],
        "available_auth_mechanisms": ["credentials"],
        "beta": false,
        "months_to_fetch": null,
        "urls": ["https://identification.adis-assurances.com", "https://www.agipi.com", "https://connexion.adis-assurances.com"],
        "siret": null,
        "hidden": false,
        "charged": true,
        "slug": "AGI",
        "categories": [],
        "name": "Agipi"
    }]
}

И я пытаюсь получить все значения "name", такие как "1&1", "aceos", "Agipi" и т. Д. *

Есть идеи, как их получить? Спасибо

1 Ответ

2 голосов
/ 21 ноября 2019

Это выглядит как неполный ответ JSON.

Попробуйте с JSON.parse, [] и map:

require 'json'

JSON.parse(foo)['banks'].map { |e| e['name'] } # ["1&1", "aceos", "Agipi"]
...