【问题标题】:Parse Using Swifty JSON使用 Swifty JSON 解析
【发布时间】:2018-08-07 05:19:00
【问题描述】:

我的JSON 是这样的:

{
    "status": 1,
    "msg": "Category Product List",
    "product_data": [{
            "product_id": "49",
            "image": "http://192.168.1.78/Linkon/site/pub/static/frontend/Linkon/default/en_US/Magento_Catalog/images/product/placeholder/image.jpg",
            "shopName": "putin",
            "review": "",
            "rating": "2",
            "productName": "ccd",
            "customrFirstName": "devi",
            "customrLastName": "ss",
            "address": "6th Ln, S.T.Colony, Mahalaxminagar, Rajarampuri, Kolhapur, Maharashtra 416008, India",
            "contactNumber": null,
            "description": "<p>ccd</p>"
        },
        {
            "product_id": "50",
            "image": "http://192.168.1.78/Linkon/site/pub/static/frontend/Linkon/default/en_US/Magento_Catalog/images/product/placeholder/image.jpg",
            "shopName": "putin",
            "review": "",
            "rating": "2",
            "productName": "car garage",
            "customrFirstName": "devi",
            "customrLastName": "ss",
            "address": "6th Ln, S.T.Colony, Mahalaxminagar, Rajarampuri, Kolhapur, Maharashtra 416008, India",
            "contactNumber": null,
            "description": "<p>car garage</p>"
        }
    ]
}

所以我的问题是:如何创建JSON 模型类并使用swifty JSON 解析?

【问题讨论】:

标签: ios swift4 swifty-json


【解决方案1】:

我建议放弃 SwiftyJSON,转而支持 Swift 4 中内置的 CodableJSONDecoder

为此,您只需定义一个与您的 JSON 格式匹配的结构,然后对其进行解码:

struct Data: Codable {
    let status: Int
    let msg: String
    let products: [Product]

    enum CodingKeys: String, CodingKey {
        case status, msg
        case products = "product_data"
    }
}

struct Product: Codable {
    let product_id, image, shopName, review: String
    let rating, productName, customrFirstName, customrLastName: String
    let address: String
    let contactNumber: String?
    let description: String
}

do {
    let data = try JSONDecoder().decode(Data.self, from: json)
    print("\(data.msg)") // e.g.
} catch {
    print("\(error)")
}

【讨论】:

    【解决方案2】:

    你可以像下面这样创建你的数据模型类:

    import UIKit
    import SwiftyJSON
    
    class ProductModels: NSObject {
       var productModel:[ProductModel]?
    }
    public init(json:JSON) {
       self.productModel = json["product_data"].dictionary
    }
    class ProductModel: NSObject {
    var productID:String?
    var image:String?
    var shopName:String?
    var review:String?
    var rating:String?
    var productName:String?
    var customrFirstName:String?
    var customrLastName:String?
    var address:String?
    var contactNumber:String?
    var description:String?
    
    public init(json:JSON) {
        self.productID = json["product_id"].string
        self. image = json["image"].string
        self. shopName = json["shopName"].string
        self. review = json["review"].string
        self. rating = json["rating"].string
        self. productName = json["productName"].string
        self. customrFirstName = json["customrFirstName"].string
        self. customrLastName = json["customrLastName"].string
        self. address = json["address"].string
        self. contactNumber = json["contactNumber"].string
        self. description = json["description"].string
        }
    }
    

    并且可以通过从调用 api 的模型传递响应并在下面获取此响应示例来使用此类:(您使用以下代码的类,您必须 import SwiftyJSON

    Alamofire.request(//(your method or api call).responseJSON(completionHandler: { (response) in
    switch response.result {
        case .success(let value):
            let productJson = JSON(value)
            let productsData = ProductModels(json: productJson)
            break;
        }
    })
    

    【讨论】:

    • @Prashant Borade 有帮助吗?
    【解决方案3】:

    您可以使用 SwiftyJSONAccelerator

    创建类

    从这里获取 SwiftyJSONAcceleratorhttps://github.com/insanoid/SwiftyJSONAccelerator

    或者您可以使用https://app.quicktype.io/在线创建它

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      相关资源
      最近更新 更多