2016-11-07 58 views
1

我有一個問題,我的JSON被輸出爲XCode中的NIL,即使我在瀏覽器中測試時報告的值是正確的。JSON_encode在瀏覽器中輸出正確,但在Swift輸出'nil'中打印(json),爲什麼?

$player1Id = array(); 
$player1Id = getPlayer1Id($player1name,$communityId); 

public function getPlayer1Id($playerName, $communityId) 
{ 

$returnValue = array(); 

$sql = "SELECT users.id\n" 
. "FROM users\n" 
. "join community_players\n" 
. "on community_players.player_id=users.id \n" 
. "WHERE users.user_name = '".$playerName."' AND community_players.community_id = '".$communityId."'"; 

$result = $this->conn->query($sql); 
    if($result != null && (mysqli_num_rows($result) >= 1)){ 
     $row = $result -> fetch_array(MYSQLI_ASSOC); 
     if(!empty($row)){ 
      $returnValue = $row['id']; 
     } 
    } 
    return $returnValue; 
} 

echo json_encode(array($player1Id)); 

如果我在瀏覽器中測試與一組中的URL值的它輸出正確:

[ 「31」]

這是我的夫特功能:

func submit(action: UIAlertAction){ 
// print (homePlayerLabel.text!,":",homeTeamLabel.text!,homeGoals,"(",player1result!,") - ", awayPlayerLabel.text!,":",awayTeamLabel.text!,awayGoals,"(",player2result!,")") 


    // Send user data to server side 

    let myUrl = URL(string: "http://www.quasisquest.uk/KeepScore/submitScore.php"); 

    var request = URLRequest(url:myUrl!); 
    request.httpMethod = "POST"; 

    let postString = "communityId=\(communityId!)&player1name=\(homePlayerLabel.text!)&player1team=\(homeTeamLabel.text!)&player1goals=\(homeGoals)&player1result=\(player1result!)&player2name=\(awayPlayerLabel.text!)&player2team=\(awayTeamLabel.text!)&player2goals=\(awayGoals)&player2result=\(player2result!)"; 

    print (postString) 
    request.httpBody = postString.data(using: String.Encoding.utf8); 


    let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in 


     DispatchQueue.main.async 
      { 


       if error != nil { 
        print("error=\(error)") 
        return 
       } 

       do { 

        let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject] 
        print (json) 

        if let parseJSON = json { 

         let returnValue = parseJSON["status"] as? String 

         if(returnValue == "Success") 
         { 
          let myAlert = UIAlertController(title: "Alert", message: "Registration successful", preferredStyle: UIAlertControllerStyle.alert); 


          let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default){(action) in 

           self.dismiss(animated: true, completion: nil) 

          } 

          myAlert.addAction(okAction); 
          self.present(myAlert, animated: true, completion: nil) 

         } else { 


          let errorMessage = parseJSON["message"] as? String 
          if(errorMessage != nil) 
          { 

           self.displayMyAlertMessage(userMessage: errorMessage!); 

          } 

         } 

        } 
       } catch{ 
        print(error) 
       } 



     } 

    } 

    task.resume() 



} 

不過當我在Xcode中報告爲:

NIL

有什麼明顯的,我俯瞰?

+0

你的反應是''的類型String'不'Dictionary' Array'所以你需要將它轉換爲'[String]'而不是'[String:Any]' –

+0

我已經將行更改爲 - 讓json = try JSONSerialization.jsonObject(with:data !, options:.allowFragments)as? [字符串:字符串] 但仍NIL – Northfield82

+0

正確檢查評論我寫了'[字符串]'不'[字符串:字符串]'。 –

回答

0

我想你更新PHP 7.1我碰到同樣的問題,同時更新PHP 7.1 try to put mysql_query('SET CHARACTER SET utf8')我認爲這是解決你的問題