0

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

4

3 回答 3

1

我建议放弃Swift 4SwiftyJSON中的内置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)")
}
于 2018-08-07T06:45:12.137 回答
0

您可以使用SwiftyJSONAccelerator创建类

在此处输入图像描述

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

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

在此处输入图像描述

于 2018-08-07T06:59:59.140 回答
0

您可以创建数据模型类,如下所示:

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;
    }
})
于 2018-08-07T06:23:35.963 回答