使用 SwiftyJson 解析多维数组

parsing array multidimension using SwiftJson

我正在尝试使用如下代码和结构解析 JSON:

"custom_attributes": [
    {
        "attribute_code": "api_attribute",
        "value": [
            {
                "color": [
                    {
                        "value_index": "4",
                        "label": "Red",
                        "product_super_attribute_id": "1",
                        "default_label": "Red",
                        "store_label": "Red",
                        "use_default_value": true
                    }
                ]
            },
            {
                "size": [
                    {
                        "value_index": "13",
                        "label": "35",
                        "product_super_attribute_id": "2",
                        "default_label": "35",
                        "store_label": "35",
                        "use_default_value": true
                    }
                ]
            },

我试过这样的代码:

Alamofire.request("http://adorableprojects.store/rest/V1/detailProduct/configurable-product").responseJSON { (responseData) -> Void in
        if((responseData.result.value) != nil) {
            let swiftyJsonVar = JSON(responseData.result.value!)
            if let resData = swiftyJsonVar["custom_attributes"]["value"]["color"].arrayObject {
                self.arrImage = resData as! [[String:AnyObject]]

但我根本没有得到 json 结果。当我尝试 if let resData = swiftyJsonVar["custom_attributes"].arrayObject 我得到所有结果

custom_attributes , value 是数组

Alamofire.request("http://adorableprojects.store/rest/V1/detailProduct/configurable-product").responseJSON { (responseData) -> Void in
    if((responseData.result.value) != nil) {

        let swiftyJsonVar = JSON(responseData.result.value!).dictionaryValue
        if let resData = swiftyJsonVar["custom_attributes"]?.arrayValue , let sec  =  resData.first?.dictionaryValue["value"]?.arrayValue , let color =  sec.first?.dictionaryValue["color"]?.arrayValue {
             print("dhjjhdhdsjhdsjdshjdsjhds   ",color) 
        }
        else {


        }

    }
}

编辑 : 访问大小

Alamofire.request("http://adorableprojects.store/rest/V1/detailProduct/configurable-product").responseJSON { (responseData) -> Void in
    if((responseData.result.value) != nil) {

        let swiftyJsonVar = JSON(responseData.result.value!).dictionaryValue
        if let resData = swiftyJsonVar["custom_attributes"]?.arrayValue , let sec  =  resData.first?.dictionaryValue["value"]?.arrayValue , let color =  sec[1].dictionaryValue["size"]?.arrayValue {
             print("dhjjhdhdsjhdsjdshjdsjhds   ",size) 
        }
        else {


        }

    }
}

顺便推荐一下

struct Root: Codable {
    let customAttributes: [CustomAttribute]

    enum CodingKeys: String, CodingKey {
        case customAttributes = "custom_attributes"
    }
}

struct CustomAttribute: Codable {
    let attributeCode: String
    let value: [Value]

    enum CodingKeys: String, CodingKey {
        case attributeCode = "attribute_code"
        case value
    }
}

struct Value: Codable {
    let color: [Color]
}

struct Color: Codable {
    let valueIndex, label, productSuperAttributeID, defaultLabel: String
    let storeLabel: String
    let useDefaultValue: Bool

    enum CodingKeys: String, CodingKey {
        case valueIndex = "value_index"
        case label
        case productSuperAttributeID = "product_super_attribute_id"
        case defaultLabel = "default_label"
        case storeLabel = "store_label"
        case useDefaultValue = "use_default_value"
    }
}

Instead of manually parsing whole response each time I would suggest you use to go for much powerful API provided by Apple to us is Codable.

您可以在此处阅读有关可编码的更多信息:https://developer.apple.com/documentation/swift/codable

您可以定义要解析的编码键并从 Codable 获取现成的模型。

编码示例:

相应地创建模型

struct Root: Codable {
    let customAttributes: [CustomAttribute]

    enum CodingKeys: String, CodingKey {
        case customAttributes = "custom_attributes"
    }
}

struct CustomAttribute: Codable {
    let attributeCode: String
    let value: [Value]

    enum CodingKeys: String, CodingKey {
        case attributeCode = "attribute_code"
        case value
    }
}

struct Value: Codable {
    let color: [Color]
}

struct Color: Codable {
    let valueIndex, label, productSuperAttributeID, defaultLabel: String
    let storeLabel: String
    let useDefaultValue: Bool

    enum CodingKeys: String, CodingKey {
        case valueIndex = "value_index"
        case label
        case productSuperAttributeID = "product_super_attribute_id"
        case defaultLabel = "default_label"
        case storeLabel = "store_label"
        case useDefaultValue = "use_default_value"
    }
}

用法:

Alamofire.request("http://adorableprojects.store/rest/V1/detailProduct/configurable-product").responseJSON { (responseData) -> Void in
                                if((responseData.result.value) != nil) {

                                    let swiftyJsonVar = JSON(responseData.result.value!)
                                    let customAttributesResponse = swiftyJsonVar["custom_attributes"]

                                    do {
                                        // You can parse response with codable's here
                                        let data = try customAttributesResponse.rawData()
                                        let customAttributes = try JSONDecoder().decode([CustomAttribute].self, from:data)
                                        print(customAttributes)
                                    }
                                    catch {
                                        debugPrint("\(#function)--\(error)")
                                    }
                                }

                            }