2016-11-07 40 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')我认为这是解决你的问题