从WCF反序列化SWIFT 2.2中的JSON

时间:2016-04-11 22:42:01

标签: ios json swift deserialization

我遇到了在swift中解析JSON的麻烦 这是我的代码:

func HTTPsendPostRequestJSon(callString: String, callBody: NSData, callback:(JSonResult) -> Void){

    let result: JSonResult = JSonResult()
    let urlString = LMVariables.baseURL + callString
    let WCFUrl = NSURL(string: urlString)
    let request: NSMutableURLRequest = NSMutableURLRequest(URL: WCFUrl!)
    request.HTTPMethod = "POST"
    request.HTTPBody = callBody
    request.addValue("application/JSON", forHTTPHeaderField: "Content-Type")
    request.addValue("application/JSON", forHTTPHeaderField: "Accept")

    let session = NSURLSession.sharedSession()

    let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

        if error != nil {
            result.err = error
            callback(result)
        } else {

            if let jsonData = try! NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary {
                result.JSon = jsonData
            }
            callback(result)                    
        }
    })

    task.resume()
}

这是我从服务器接收的JSON(将NSData转换为字符串时):

{
    "AccountTypeID": null,
    "BirthDate": null,
    "ErrorID": 268435456,
    "GSM": "",
    "GenderID": null,
    "IsActive": null,
    "IsConfirmed": null,
    "LaiksActive": null,
    "LangID": null,
    "LastActiveEventID": null,
    "LastActiveEventIsFinished": null,
    "LogonToken": null,
    "NewAccount": false,
    "PNToken": "",
    "PW": "",
    "ResourceUri": null,
    "USREmail": "",
    "USRFirstName": "",
    "USRLastName": ""
}

但问题在于NSJSONSerialization.JSONObjectWithData电话。 这是反序列化后字典的样子: (这里很多字段都是空的,但是当我的字段包含数据时,我的结果相同)

jsonData    NSDictionary    19 key/value pairs  0x00007fcf14809080
[0] (null)  "LastActiveEventID" : (no summary)
[1] (null)  (no summary) : Int64(268435456)
[2] (null)  "NewAccount" : "0"
[3] (null)  "ResourceUri" : (no summary)
[4] (null)  "LastActiveEventIsFinished" : (no summary)
[5] (null)  "LogonToken" : (no summary)
[6] (null)  (no summary) : ""
[7] (null)  (no summary) : (no summary)
[8] (null)  (no summary) : (no summary)
[9] (null)  (no summary) : ""
[10]    (null)  (no summary) : ""
[11]    (null)  "USRFirstName" : ""
[12]    (null)  (no summary) : ""
[13]    (null)  (no summary) : (no summary)
[14]    (null)  (no summary) : (no summary)
[15]    (null)  (no summary) : (no summary)
[16]    (null)  "USRLastName" : ""
[17]    (null)  "AccountTypeID" : (no summary)
[18]    (null)  "LaiksActive" : (no summary)

正如您所看到的,许多密钥丢失并替换为(无摘要)。

奇怪的是,这个代码之前在swift 2.0中有效。

非常感谢任何帮助。

最佳, 埃里克

0 个答案:

没有答案