0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JSONを簡単に扱う為のライブラリ SwiftyJSON

Last updated at Posted at 2020-06-13

使うAPI

{
result: [
{
foodImageUrl: "https://image.space.rakuten.co.jp/d/strg/ctrl/3/fbd7dd260d736654532e6c0b1ec185a0cede8675.49.2.3.2.jpg",
recipeDescription: "そのままでも、ご飯にのせて丼にしても♪",
recipePublishday: "2017/10/10 22:37:34",
shop: 0,
pickup: 0,
recipeId: 1760028309,
nickname: "はぁぽじ",
smallImageUrl: "https://image.space.rakuten.co.jp/d/strg/ctrl/3/fbd7dd260d736654532e6c0b1ec185a0cede8675.49.2.3.2.jpg?thum=55",
recipeMaterial: [],
recipeIndication: "約10分",
recipeCost: "300円前後",
rank: "1",
recipeUrl: "https://recipe.rakuten.co.jp/recipe/1760028309/",
mediumImageUrl: "https://image.space.rakuten.co.jp/d/strg/ctrl/3/fbd7dd260d736654532e6c0b1ec185a0cede8675.49.2.3.2.jpg?thum=54",
recipeTitle: "ご飯がすすむ!鶏むね肉のねぎ塩焼き"
},
]

楽天レシピAPI

普通にfoodImageUrl、recipeTitleを取り出すには

import UIKit

struct ResultList: Codable {
    let result: [User]
 }

struct User: Codable {
    let foodImageUrl :String
    let recipeTitle  :String
}
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
      getRApi()
    }
     private func getRApi(){
            guard let url = URL(string: "APIのurl") else {return}
            
            let task = URLSession.shared.dataTask(with: url) { (data, response, err)in
                if let err = err {
                    print("情報の取得に失敗しました。:", err)
                    return
                }
                if let data = data{
                    do{
                        let resultList = try JSONDecoder().decode(ResultList.self, from: data)
                        print("json: ", resultList)
                    }catch(let err){
                         print("情報の取得に失敗しました。:", err)
                    }
                }
            }
            task.resume()
        }
    }

長い。

SwiftyJSON

import UIKit
import SwiftyJSON

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "APIのurl")

        URLSession.shared.dataTask(with: url!) { (data, response, error) in
            guard let data = data else {return}
            do {
                let json = try? JSON(data: data)
                let foodImageUrl = json!["result"][0]["foodImageUrl"]
                let recipeTitle = json!["result"][0]["recipeTitle"]
                print(recipeTitle,foodImageUrl)
            } catch let jsonError{
                print("jsonError", jsonError)
            }
            }.resume()
    }
}

すっきり!!

ネットで一つのデータを取り出す方法の例は沢山あったので、なんとなく複数のデータを取り出す例で記事書かせていただきました。
またAPI勉強してまもないのでおかしなところはあると思いますが、記録として残しておきます。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?