首页 > 解决方案 > 使用 Swift 5 和 Alomofire 自动解析 JSON

问题描述

我在 Swift 5 中通过 Alomofire 调用 API 并获得以下 JSON 格式的结果集:

这是电话:

AF.request("https://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemSearch", parameters: parameters).responseJSON { response in
        print(response.result)
    }

我在结果中有以下信息: Content-Encoding: gzip,解决方案可能来自这里)

这是 JSON 响应正文:

{"ResultSet":{"totalResultsAvailable":"3","totalResultsReturned":3,"firstResultPosition":"1","0":{"Result":{"Request":{"Query":""},"Modules":"","0":{"Name":"\u3010\u30ad\u30e3\u30c3\u30b7\u30e5\u30ec\u30b95\uff05\u9084\u5143\u3011\u30ab\u30eb\u30d3\u30fc\u3000\u304b\u3063\u3071\u3048\u3073\u305b\u3093\u7d00\u5dde\u306e\u6885\u547370g\u3000\u3010\u30a4\u30fc\u30b8\u30e3\u30d1\u30f3\u30e2\u30fc\u30eb\u3011","Description":"\u3010\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\uff08\u5546\u54c1\u5185\u5bb9\u3092\u4fdd\u969c\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff09\u3011\u304a\u83d3\u5b50\u3000\u304a\u3084\u3064\u3000\u30b9\u30ca\u30c3\u30af\u3000\u30b9\u30ca\u30c3\u30af\u83d3\u5b50\u3000\u30ab\u30c3\u30d1\u30a8\u30d3\u30bb\u30f3\u3000\u3046\u3081\u5473\u3000\u30a6\u30e1\u3000\u6d77\u8001\u3000\u30ab\u30eb\u30b7\u30a6\u30e0","Headline":"","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/4901330197711f.html","ReleaseDate":"","Availability":"instock","Code":"ejapan_4901330197711f","Condition":"new","Image":{"Id":"ejapan_4901330197711f","Small":"https:\/\/item-shopping.c.yimg.jp\/i\/c\/ejapan_4901330197711f","Medium":"https:\/\/item-shopping.c.yimg.jp\/i\/g\/ejapan_4901330197711f"},"Review":{"Rate":"0.00","Count":"0","Url":"https:\/\/shopping.yahoo.co.jp\/review\/item\/list?store_id=ejapan&page_key=4901330197711f"},"Affiliate":{"Rate":"1.0"},"Price":{"_attributes":{"currency":"JPY"},"_value":"121"},"PremiumPrice":"","PriceLabel":{"_attributes":{"taxIncluded":"true"},"FixedPrice":"","DefaultPrice":"121","SalePrice":"","PremiumPriceStatus":"0","PremiumPrice":"121","PremiumDiscountType":"","PremiumDiscountRate":"","PeriodStart":"","PeriodEnd":""},"Point":{"Amount":"1","Times":"1","PremiumAmount":"1","PremiumTimes":"1"},"Shipping":{"Code":"1","Name":"\u8a2d\u5b9a\u7121\u3057"},"Category":{"Current":{"Id":"13451","Name":"\u305d\u306e\u4ed6\u30b9\u30ca\u30c3\u30af\u3001\u304a\u83d3\u5b50\u3001\u304a\u3064\u307e\u307f"}},"CategoryIdPath":{"0":{"Id":"1"},"1":{"Id":"2498"},"2":{"Id":"4745"},"3":{"Id":"13451"},"_container":"Category"},"Brands":{"Name":"\u30ab\u30eb\u30d3\u30fc","Path":{"0":{"Id":"1"},"1":{"Id":"16267"},"_container":"Brand"}},"JanCode":"4901330197711","Model":"","IsbnCode":"","Store":{"Id":"ejapan","Name":"e\u30b8\u30e3\u30d1\u30f3","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/","Payment":{"0":{"Code":"1","Name":"\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"1":{"Code":"16","Name":"Yahoo!\u30a6\u30a9\u30ec\u30c3\u30c8\u306b\u767b\u9332\u3057\u3066\u3044\u308b\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"2":{"Code":"4","Name":"\u5546\u54c1\u4ee3\u5f15"},"3":{"Code":"2","Name":"\u9280\u884c\u632f\u8fbc"},"4":{"Code":"8","Name":"\u90f5\u4fbf\u632f\u66ff"},"5":{"Code":"1024","Name":"\u30bd\u30d5\u30c8\u30d0\u30f3\u30af\u307e\u3068\u3081\u3066\u652f\u6255\u3044"},"6":{"Code":"4096","Name":"PayPay"},"_container":"Method"},"IsBestStore":"false","Ratings":{"Rate":"4.5","Count":"11871","Total":"51092","DetailRate":"4.3"},"Image":{"Id":"ejapan_1","Medium":"https:\/\/item-shopping.c.yimg.jp\/s\/h\/ejapan_1"},"IsPMallStore":"false"},"IsAdult":"0","Deliveryinfo":{"Area":"","Deadline":"","Day":""},"_attributes":{"index":"1"}},"1":{"Name":"\u3010\u30ad\u30e3\u30c3\u30b7\u30e5\u30ec\u30b95\uff05\u9084\u5143\u3011\u2605\u307e\u3068\u3081\u8cb7\u3044\u2605\u3000\u30ab\u30eb\u30d3\u30fc\u3000\u304b\u3063\u3071\u3048\u3073\u305b\u3093\u7d00\u5dde\u306e\u6885\u547370g\u3000\u3000\u00d712\u500b\u3010\u30a4\u30fc\u30b8\u30e3\u30d1\u30f3\u30e2\u30fc\u30eb\u3011","Description":"\u3010\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\uff08\u5546\u54c1\u5185\u5bb9\u3092\u4fdd\u969c\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff09\u3011\u304a\u83d3\u5b50\u3000\u304a\u3084\u3064\u3000\u30b9\u30ca\u30c3\u30af\u3000\u30b9\u30ca\u30c3\u30af\u83d3\u5b50\u3000\u30ab\u30c3\u30d1\u30a8\u30d3\u30bb\u30f3\u3000\u3046\u3081\u5473\u3000\u30a6\u30e1\u3000\u6d77\u8001\u3000\u30ab\u30eb\u30b7\u30a6\u30e0","Headline":"","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/012-4901330197711f.html","ReleaseDate":"","Availability":"instock","Code":"ejapan_012-4901330197711f","Condition":"new","Image":{"Id":"ejapan_012-4901330197711f","Small":"https:\/\/item-shopping.c.yimg.jp\/i\/c\/ejapan_012-4901330197711f","Medium":"https:\/\/item-shopping.c.yimg.jp\/i\/g\/ejapan_012-4901330197711f"},"Review":{"Rate":"0.00","Count":"0","Url":"https:\/\/shopping.yahoo.co.jp\/review\/item\/list?store_id=ejapan&page_key=012-4901330197711f"},"Affiliate":{"Rate":"1.0"},"Price":{"_attributes":{"currency":"JPY"},"_value":"1398"},"PremiumPrice":"","PriceLabel":{"_attributes":{"taxIncluded":"true"},"FixedPrice":"","DefaultPrice":"1398","SalePrice":"","PremiumPriceStatus":"0","PremiumPrice":"1398","PremiumDiscountType":"","PremiumDiscountRate":"","PeriodStart":"","PeriodEnd":""},"Point":{"Amount":"13","Times":"1","PremiumAmount":"13","PremiumTimes":"1"},"Shipping":{"Code":"1","Name":"\u8a2d\u5b9a\u7121\u3057"},"Category":{"Current":{"Id":"13451","Name":"\u305d\u306e\u4ed6\u30b9\u30ca\u30c3\u30af\u3001\u304a\u83d3\u5b50\u3001\u304a\u3064\u307e\u307f"}},"CategoryIdPath":{"0":{"Id":"1"},"1":{"Id":"2498"},"2":{"Id":"4745"},"3":{"Id":"13451"},"_container":"Category"},"Brands":{"Name":"\u30ab\u30eb\u30d3\u30fc","Path":{"0":{"Id":"1"},"1":{"Id":"16267"},"_container":"Brand"}},"JanCode":"4901330197711","Model":"","IsbnCode":"","Store":{"Id":"ejapan","Name":"e\u30b8\u30e3\u30d1\u30f3","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/","Payment":{"0":{"Code":"1","Name":"\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"1":{"Code":"16","Name":"Yahoo!\u30a6\u30a9\u30ec\u30c3\u30c8\u306b\u767b\u9332\u3057\u3066\u3044\u308b\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"2":{"Code":"4","Name":"\u5546\u54c1\u4ee3\u5f15"},"3":{"Code":"2","Name":"\u9280\u884c\u632f\u8fbc"},"4":{"Code":"8","Name":"\u90f5\u4fbf\u632f\u66ff"},"5":{"Code":"1024","Name":"\u30bd\u30d5\u30c8\u30d0\u30f3\u30af\u307e\u3068\u3081\u3066\u652f\u6255\u3044"},"6":{"Code":"4096","Name":"PayPay"},"_container":"Method"},"IsBestStore":"false","Ratings":{"Rate":"4.5","Count":"11875","Total":"51100","DetailRate":"4.3"},"Image":{"Id":"ejapan_1","Medium":"https:\/\/item-shopping.c.yimg.jp\/s\/h\/ejapan_1"},"IsPMallStore":"false"},"IsAdult":"0","Deliveryinfo":{"Area":"","Deadline":"","Day":""},"_attributes":{"index":"2"}},"2":{"Name":"\u3010\u30ad\u30e3\u30c3\u30b7\u30e5\u30ec\u30b95\uff05\u9084\u5143\u3011\u3010\u9001\u6599\u7121\u6599\u3011\u2605\u307e\u3068\u3081\u8cb7\u3044\u2605\u3000\u30ab\u30eb\u30d3\u30fc\u3000\u304b\u3063\u3071\u3048\u3073\u305b\u3093\u7d00\u5dde\u306e\u6885\u547370g\u3000\u3000\u00d712\u500b\u3010\u30a4\u30fc\u30b8\u30e3\u30d1\u30f3\u30e2\u30fc\u30eb\u3011","Description":"\u3010\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\uff08\u5546\u54c1\u5185\u5bb9\u3092\u4fdd\u969c\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff09\u3011\u304a\u83d3\u5b50\u3000\u304a\u3084\u3064\u3000\u30b9\u30ca\u30c3\u30af\u3000\u30b9\u30ca\u30c3\u30af\u83d3\u5b50\u3000\u30ab\u30c3\u30d1\u30a8\u30d3\u30bb\u30f3\u3000\u3046\u3081\u5473\u3000\u30a6\u30e1\u3000\u6d77\u8001\u3000\u30ab\u30eb\u30b7\u30a6\u30e0","Headline":"","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/012-4901330197711fm.html","ReleaseDate":"","Availability":"instock","Code":"ejapan_012-4901330197711fm","Condition":"new","Image":{"Id":"ejapan_012-4901330197711fm","Small":"https:\/\/item-shopping.c.yimg.jp\/i\/c\/ejapan_012-4901330197711fm","Medium":"https:\/\/item-shopping.c.yimg.jp\/i\/g\/ejapan_012-4901330197711fm"},"Review":{"Rate":"0.00","Count":"0","Url":"https:\/\/shopping.yahoo.co.jp\/review\/item\/list?store_id=ejapan&page_key=012-4901330197711fm"},"Affiliate":{"Rate":"1.0"},"Price":{"_attributes":{"currency":"JPY"},"_value":"2278"},"PremiumPrice":"","PriceLabel":{"_attributes":{"taxIncluded":"true"},"FixedPrice":"","DefaultPrice":"2278","SalePrice":"","PremiumPriceStatus":"0","PremiumPrice":"2278","PremiumDiscountType":"","PremiumDiscountRate":"","PeriodStart":"","PeriodEnd":""},"Point":{"Amount":"22","Times":"1","PremiumAmount":"22","PremiumTimes":"1"},"Shipping":{"Code":"1","Name":"\u8a2d\u5b9a\u7121\u3057"},"Category":{"Current":{"Id":"13451","Name":"\u305d\u306e\u4ed6\u30b9\u30ca\u30c3\u30af\u3001\u304a\u83d3\u5b50\u3001\u304a\u3064\u307e\u307f"}},"CategoryIdPath":{"0":{"Id":"1"},"1":{"Id":"2498"},"2":{"Id":"4745"},"3":{"Id":"13451"},"_container":"Category"},"Brands":{"Name":"\u30ab\u30eb\u30d3\u30fc","Path":{"0":{"Id":"1"},"1":{"Id":"16267"},"_container":"Brand"}},"JanCode":"4901330197711","Model":"","IsbnCode":"","Store":{"Id":"ejapan","Name":"e\u30b8\u30e3\u30d1\u30f3","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/","Payment":{"0":{"Code":"1","Name":"\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"1":{"Code":"16","Name":"Yahoo!\u30a6\u30a9\u30ec\u30c3\u30c8\u306b\u767b\u9332\u3057\u3066\u3044\u308b\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"2":{"Code":"4","Name":"\u5546\u54c1\u4ee3\u5f15"},"3":{"Code":"2","Name":"\u9280\u884c\u632f\u8fbc"},"4":{"Code":"8","Name":"\u90f5\u4fbf\u632f\u66ff"},"5":{"Code":"1024","Name":"\u30bd\u30d5\u30c8\u30d0\u30f3\u30af\u307e\u3068\u3081\u3066\u652f\u6255\u3044"},"6":{"Code":"4096","Name":"PayPay"},"_container":"Method"},"IsBestStore":"false","Ratings":{"Rate":"4.5","Count":"11874","Total":"51099","DetailRate":"4.3"},"Image":{"Id":"ejapan_1","Medium":"https:\/\/item-shopping.c.yimg.jp\/s\/h\/ejapan_1"},"IsPMallStore":"false"},"IsAdult":"0","Deliveryinfo":{"Area":"","Deadline":"","Day":""},"_attributes":{"index":"3"}},"_container":"Hit"}}}}

所以我想找到自动解析这个JSON主体的方法,以便以后利用数据,

提前谢谢你,查尔斯

标签: jsonapigzipswift5

解决方案


在解码和保存来自 REST API 的 JSON 响应的数据时,我使用了以下方法:

  • 包括以下两个 PODS

    1) 阿拉莫菲尔

    2) 波场

现在,您必须创建一个类,用作 API 和实际类模型之间的数据源接口。在下面的示例中,我从电影数据库服务中提取数据,并且我有一个用于数据源的波纹管类:

class MovieDataSource: JSONDecodable {

    var movies: [Movie] = []

    let cellid = "cellid"

    required init(json: JSON) throws {

        guard let moviesArray = json["results"].array  else {

            print("Error on the JSON Format")

            return
        }

        self.movies = moviesArray.map({return Movie(json: $0)})
    }
}


class JSONError: JSONDecodable {

    required init(json: JSON) throws {

        print("JSON error\n", json)

    }
}

然后我有实际的数据保存类/结构:

struct Movie {

    let title: String

    let movieId: Int

    let overview: String

    let releaseDate: String

    let posterPathURL: String

    let genres: [Int]

    let vote_average: Float

    let popularity: Float

    let isForAdults: Bool


    init(json: JSON) {

        self.title = json["title"].stringValue

        self.movieId = json["id"].intValue

        self.overview = json["overview"].stringValue

        self.releaseDate = json["release_date"].stringValue

        self.posterPathURL = json["poster_path"].stringValue

        self.genres = json["genre_ids"].arrayObject as! [Int]

        self.vote_average = json["vote_average"].floatValue

        self.popularity = json["popularity"].floatValue

        self.isForAdults = json["adult"].boolValue

    }

}


我认为以上信息应该足以让您了解如何继续前进,但如果您需要进一步的指导,我发现这个视频系列对这个主题非常有教育意义:https ://www.youtube.com/watch?v =tt67VikEovY&list=PL0dzCUj1L5JE1wErjzEyVqlvx92VN3DL5&index=13

祝你好运!


推荐阅读