使用 Swifty JSON 解析

Posted

技术标签:

【中文标题】使用 Swifty JSON 解析【英文标题】:Parse Using 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 解析?

【问题讨论】:

你可以使用这个github.com/Ahmed-Ali/JSONExport来生成类。 【参考方案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

创建类

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

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

【讨论】:

以上是关于使用 Swifty JSON 解析的主要内容,如果未能解决你的问题,请参考以下文章

Swifty JSON 响应解析

Swifty-JSON:无法解析下划线

如何使用 Swifty 动态创建具有多个键和值的 json 对象

swifty json 解析数组对象

swifty Json 解析数据显示我为零?

Swift 3 Alamofire Swifty json 解析问题