Разбор массива многомерный с использованием SwiftyJson

Если вы используете Rails, вы можете просто сделать пустой файл в общей папке и использовать ajax для его получения. Затем проанализируйте заголовки для заголовка Date. Файлы в общей папке обходят стек Rails и имеют более низкую задержку.

0
задан Irwan Madness 18 January 2019 в 03:56
поделиться

2 ответа

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"
    }
}
0
ответ дан Sh_Khan 18 January 2019 в 03:56
поделиться

Вместо того, чтобы вручную анализировать весь ответ каждый раз, я бы посоветовал вам воспользоваться мощным API-интерфейсом, предоставленным нам компанией Apple, - 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"
    }
}

Использование: [119 ]

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)")
                                    }
                                }

                            }
0
ответ дан Jarvis The Avenger 18 January 2019 в 03:56
поделиться
Другие вопросы по тегам:

Похожие вопросы: