簡體   English   中英

如何在 iOS Swift 中使用 alamofire 獲得 json 響應?

[英]How to get json response using alamofire in iOS Swift?

我一直在嘗試使用 alamofire 從 url 獲得 json 響應。 創建了 model、apirouter 和 api 客戶端 class。

它顯示錯誤

 failure(Alamofire.AFError.responseSerializationFailed(reason: 
  Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: 
   Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "variables", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"variables\", intValue: nil) (\"variables\").", underlyingError: nil)))))

這是我的 postman json 回復:

   [
{
    "id": "00602c70-fc8a-11e9-ad1d-2abe2670111d",
    "resourceType": "Task",
    "name": "My Tasks",
    "owner": null,
    "query": {
        "assigneeExpression": "${currentUser()}",
        "taskVariables": [],
        "processVariables": [],
        "caseInstanceVariables": [],
        "orQueries": []
    },
    "properties": {
        "variables": [
            {
                "name": "loanAmount",
                "label": "Loan Amount"
            },
            {
                "name": "firstName",
                "label": "First Name"
            }
        ],
        "color": "#555555",
        "showUndefinedVariable": false,
        "description": "Tasks assigned to me",
        "refresh": false,
        "priority": -10
      }
    }
   ]

我嘗試從 json 響應中獲取id、name 和 properties -> variables -> name 和 label的值。

這是 model class:

  import Foundation

public struct Filter: Codable {

let id: String
let name: String
let properties: [variables]


}

public struct variables: Codable {

   let name: String
   let label: String

}

這是alamofire的代碼:

    private static func performRequest<T:Decodable>(route:APIRouter, decoder: JSONDecoder = JSONDecoder(), completion:@escaping (AFResult<T>)->Void) -> DataRequest {

    return AF.request(route)
                    .responseDecodable (decoder: decoder){ (response: AFDataResponse<T>) in
                        completion(response.result)
                        print("framework response::",response.result)


    }


}


  public static func getFilter(completion:@escaping (AFResult<[Filter]>)->Void) {
       let jsonDecoder = JSONDecoder()
    performRequest(route: APIRouter.getFilter, decoder: jsonDecoder, completion: completion)
   }

任何幫助非常感謝...

您的 model class 應該如下所示。

import Foundation

public struct Filter: Codable {

let id: String
let name: String
let properties: Properties


}

public struct Properties: Codable {
   let variables: [variables]
   let color: String
   let showUndefinedVariable: Bool
   let description: String
   let refresh: Bool
   let priority: Int

}

public struct variables: Codable {

   let name: String
   let label: String

}

你試過這個嗎? 這是應該的

public struct Filter: Codable {
    let id: String
    let name: String
    let properties: Property
}

public struct Property: Codable {
    let variables: [Variable]
}

public struct Variable: Codable {
    let name: String
    let label: String

}

您收到的錯誤消息非常清楚: No value associated with key CodingKeys(stringValue: \"variables\"

您正在嘗試將 JSON 解碼為Filter結構,但 JSON 沒有variables屬性。 您可以通過引入一個新的Properties結構來解決此問題,該結構將包裝variables屬性,如下所示:

struct Filter: Codable {
    let id: String
    let name: String
    let properties: Properties
}

struct Properties: Codable {
    let variables: Variables
}

struct Variables: Codable {
    let name: String
    let label: String
}

此外,正如您將在此代碼段中看到的那樣,在 CamelCase 中編寫類型名稱是慣例,因此使用struct Variables而不是struct variables

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM