JSonDecoder()Swift 4解码错误

时间:2017-08-11 07:42:10

标签: json xcode

尝试在swift 4中解码Json时出现错误

typeMismatch(Swift.Dictionary,Swift.DecodingError.Context(codingPath:[Swift._DictionaryCodingKey(stringValue:“ThrottleSeconds”,intValue:nil)],debugDescription:“预计会解码字典而是找到一个数字。”,underlyingError :零))

Json的回复是

{ “响应”: “4611686018456390681”, “错误码”:1, “ThrottleSeconds”:0 “的ErrorStatus”: “成功”, “消息”: “OK”, “MessageData”:{}}

struct BasicReply: Decodable {

let Response : String?
let ErrorCode : Int?
let ThrottleSeconds : Int?
let ErrorStatus : String?
let Message : String?
let MessageData : String?
}

class NetworkRequests {
let api = apiDetails()


func buildRequests()->String{

    let request = "1/Stats/GetMembershipIdByDisplayName/" + api.userName + "/"

   // let request = "1/Account/" + api.membershipId + "/Summary/"
    return request
}
func request(){


    let destinyURL : String = api.destinyURL + buildRequests()
    let url = NSURL(string: destinyURL)
    let request = NSMutableURLRequest(url: url! as URL)
    request.httpMethod = "GET"
    // add headers
    request.setValue(api.apiKey, forHTTPHeaderField: "X-API-Key")

    let session = URLSession.shared

    let destinySession = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
        if let response = response as? HTTPURLResponse {
            let code = response.statusCode
            print(code)
        }

        guard let data = data else {print("No DATA!!!");return}
        let output = String(data: data, encoding: String.Encoding.utf8)
        print("output ", output! as Any)

        do {

            let questions = try JSONDecoder().decode([String: BasicReply].self, from: data)

        } catch let jsonErr {

            print("Error decoding Json Questons", jsonErr)
        }
    }
    destinySession.resume()
}
}

0 个答案:

没有答案