[Swift] Use Swity JSON to easily get information from JSON data of the fortune-telling API

There was an API that provided fortune-telling for free, so when I tried to call it with Swift, the name of the JSON child element was confused in a format like date (2021/01/01). It seems that it can be solved by using Swifty JSON, so I tried it.

Development environment

Xcode 12.3 SwiftyJSON 5.0.0

Get information from API using SwiftyJSON

As an example, get the Aries love luck on January 05, 2021

import UIKit
import SwiftyJSON

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        getHoroscopeDate()
    }

    private func getHoroscopeDate() {
        
        guard let url = URL(string: "http://api.jugemkey.jp/api/horoscope/free/2021/01/03") else { return }
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            if let error = error {
                print("error", error)
                return
            }
            guard let data = data else {
                print("Failed to get Data information")
                return
            }
            do {
                let json = try? JSON(data: data)
                if let json = json {
                    let love = json["horoscope"]["2021/01/05"][0]["love"]
                    print(love)
                }
            }
        }
        task.resume()
    }
}

I was able to get the desired information fairly easily without having to create a structure for the API.

JSON data

Acquisition result (2021/01/05)

{
    "horoscope": {
        "2021/01/05": [
        {
        "content": "The time you spend in the library will enhance your imagination. If you're having trouble, read a book by your favorite author.",
        "item": "casserole",
        "money": 4,
        "total": 3,
        "job": 3,
        "color": "green",
        "day": 5,
        "love": 3,
        "rank": 7,
        "sign": "Aries"
        },

.......Below are 11 constellations

        ]
    }
}

reference

SwiftyJSON

API used

Web ad Fortune Free API There are conditions of use, so please use the paid version for commercial use.

Summary

Using Swifty JSON made it really easy to get information from JSON data. It's a very useful library, so I want to deepen my understanding.

Recommended Posts

[Swift] Use Swity JSON to easily get information from JSON data of the fortune-telling API
How to get the longest information from Twitter as of 12/12/2016
[Swift5] Get array (individual) information from JSON using library'SwiftyJSON'
[Swift] Use UserDefaults to save data in the app
[Swift] How to get the document ID of Firebase
Get to the abbreviations from 5 examples of iterating Java lists
[Swift5] How to analyze complex JSON and get the index of the element that satisfies the condition
[Swift5] How to get an array and the complement of arrays
[Swift UI] How to get the startup status of the application [iOS]
From introduction to use of ActiveHash
[Rails] Use devise to get information about the currently logged in user
[IOS] How to get data from DynamoDB
[Swift] Get the height of Safe Area
[Swift] How to get the number of elements in an array (super basic)
How to get the ID of a user authenticated with Firebase in Swift
[Java] How to easily get the longest character string of ArrayList using stream
I want to get only the time from Time type data ...! [Strftime] * Additional notes
Add empty data to the top of the list
How to get today's day of the week
Output of how to use the slice method
[Java] How to get the authority of the folder
Use remotes :: install_github ("github repository name") to get resources from GitHub to the R runtime environment
[Rails] Let's dynamically get the threshold of model validation "length check" from table information
[Java] How to get the URL of the transition source
[Swift] Get the number of steps with CMP edometer
From the introduction of devise to the creation of the users table
How to write Scala from the perspective of Java
The story of migrating from Paperclip to Active Storage
[Ruby] Method to easily get the receiver type .class
Use Java lambda expressions outside of the Stream API
[Java] How to get the maximum value of HashMap
Until the use of Spring Data and JPA Part 2
Summary of Java communication API (1) How to use Socket
[Android] How to get the setting language of the terminal
Until the use of Spring Data and JPA Part 1
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
[Rails] How to get the contents of strong parameters
Java: Use Stream to sort the contents of the collection
Generate models from JSON to Swift, PHP, C #, JAVA
[Ruby On Rails] How to search and save the data of the parent table from the child table