2017-07-21 215 views
0

我有一個快速的代碼我的問題。我通過httpMethod POST向web服務器發出請求。這個請求沒問題。我在數據值中獲得響應和數據。數據看起來像JSONHTTP請求GET JSON並讀取數據

{"pushValues": {"devicePushGlobal":"1","devicePushNewProducts":"1","devicePushNewOffer":"1"}} 

然後我會加載這個響應數據來設置基於響應數據的按鈕。但我沒有寫這個代碼。有人能幫助我嗎? :)

錯誤代碼 Cannot invoke 'jsonObject' with an argument list of type '(with: NSString)' //我測試了其他的選擇,但我總是失敗:-(

我評論了錯誤代碼....

let url = "https://URL.php" 
let request = NSMutableURLRequest(url: NSURL(string: url)! as URL) 
let bodyData = "token=" + (dts) 
request.httpMethod = "POST" 
request.httpBody = bodyData.data(using: String.Encoding.utf8); 
NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue.main) { 
    (response, data, error) in 

    // here i get the result of 
    // {"pushValues": {"devicePushGlobal":"1","devicePushNewProducts":"1","devicePushNewOffer":"1"}} 
    var str = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 
    var names = [String]() 
    // here i will get each value of pushValues to add to the array names 
    do { 
     if let data = str, 
     // ... and here is the error code by xcode ::: ==> Cannot invoke 'jsonObject' with an argument list of type '(with: NSString)' 
     // i tested with other options but i always fail :-(
     let json = try JSONSerialization.jsonObject(with: data) as? [String: Any], 
     let blogs = json["pushValues"] as? [[String: Any]] { 
      for blog in blogs { 
       if let name = blog["devicePushGlobal"] as? String { 
        print(name) 
        names.append(name) 
       } 
      } 
     } 
    } catch { 
     print("Error deserializing JSON: \(error)") 
    } 
    // names array is empty 
    print(names) 
} 

謝謝你您的幫助

+0

刪除'如果讓數據= str'。 – Larme

回答

1

您不應該使用var str = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)將JSON響應解碼爲NSStringJSONSerialization.jsonObject()需要將Data對象作爲輸入arg ument,所以才放心地解開可選data變量和使用,作爲輸入參數:

if let responesData = data, let json = try JSONSerialization.jsonObject(with: responseData) as? [String: Any], let blogs = json["pushValues"] as? [String: Any]

使用本地斯威夫特類型的完整代碼:

... 
let request = URLRequest(url: URL(string: url)!) 
... 
URLSession.shared.dataTask(with: request, completionHandler: { 
    (response, data, error) in 
    var names = [String]() 
    do { 
     if let responseData = data, let json = try JSONSerialization.jsonObject(with: responseData) as? [String: Any], let blogs = json["pushValues"] as? [String: Any]{ 
      if let name = blog["devicePushGlobal"] as? Int { 
       print(name) 
       names.append(name) 
      } 
      if let newProducts = blog["devicePushNewProducts"] as? Int{} 
      if let newOffers = blog["devicePushNewOffers"] as? Int{} 
     } 
    } catch { 
     print("Error deserializing JSON: \(error)") 
    } 
    // names array is empty 
    print(names) 
}).resume() 
+0

我在控制檯沒有輸出,查看代碼中的輸出..........''NSURLConnection.sendAsynchronousRequest(request as URLRequest,queue:OperationQueue.main){ (response,data,error)in var names = [String ]() do { }打印(數據)// CONSOLE ===>可選(93字節)** 如果讓responseData = data,讓json = try JSONSerialization.jsonObject(with:responseData)as? [String:Any],讓blogs = json [「pushValues」]爲? [[String:Any]] { ** print(「+++」)// CONSOLE ===> no output,may json format wrong?** for blog in blogs {' – MichaTz

+0

Please include the JSON你(希望)在你的問題中收到的回覆,也許它的格式是錯誤的。 –

+0

{「pushValues」:{「devicePushGlobal」:「0」,「devicePushNewProducts」:「0」,「devicePushNewOffer」:「0」}} – MichaTz