mike kumawat
mike kumawat

Reputation: 165

How to get values of an array of dictionary from alamofire response using swift3

I'm trying to extract "translations" Array "text" and "verses" array "verse_key" data from below json response using Alamofire and swift3.

{
  "verses": [
    {
      "id": 1,
      "verse_number": 1,
      "chapter_id": 1,
      "verse_key": "1:1",
      "text_madani": "بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ",
      "text_indopak": "بِسْمِ اللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ",
      "text_simple": "بسم الله الرحمن الرحيم",
      "juz_number": 1,
      "hizb_number": 1,
      "rub_number": 1,
      "sajdah": null,
      "sajdah_number": null,
      "page_number": 1,
      "audio": {
        "url": "versesAbdulBaset/Mujawwad/mp3/001001.mp3",
        "duration": 6,
      
        ],
        "format": "mp3"
      },
      "translations": [
        {
          "id": 102574,
          "language_name": "english",
          "text": "In the name of Allah, the Beneficent, the Merciful.",
          "resource_name": "Shakir",
          "resource_id": 21
        }
      ],
            }
  ],
  "meta": {
    "current_page": 1,
    "next_page": null,
    "prev_page": null,
    "total_pages": 1,
    "total_count": 7
  }
}
I'm new to swift and I can't find a way to achieve this. How can I get the values of "translations" Array "text" and "verses" array "verse_key" ? thanks advance

Upvotes: 0

Views: 108

Answers (1)

Faheem Rahman
Faheem Rahman

Reputation: 360

Use swiftyJSON.

 switch response.result{
    case .success(let data) :
    let json = JSON(data)
    let verses = json["verses"].Stringvalue
    print(verses)  //get all verses
    print(verses["verse_key"].Stringvalue)  // get "verse_key" 
    break

You can take each values from this json by giving the key names. If you want to get the "verses" , use json["verses"].You can also use JSONdecoder.

Upvotes: 1

Related Questions