我是swift编程的初学者,我正在研究项目,我必须从远程mysql数据库中检索数据,并且我在Json中遇到此错误
jsonResult:Error Domain = NSCocoaErrorDomain Code = 3840"字符0周围的值无效。" UserInfo = {NSDebugDescription =字符0周围的值无效。}
这是我的php文件:
<?php
header("Content-Type: application/json");
$host = 'localhost';
$mysql_username = 'mm';
$mysql_password = 'aa';
$database = 'aa';
$connect = new mysqli($host, $mysql_username, $mysql_password,
$database);
if (mysqli_connect_errno()){
die("cannot connect to database".mysqli_connect_error());
}
echo 'Connected successfully';
$latitude = $_REQUEST["latitude"];
$longitude= $_REQUEST["longitude"];
$myArray = array();
$sql = "SELECT * , (6371 * 2 * ASIN(SQRT( POWER(SIN(( $latitude -
Latitude) * pi()/180 / 2), 2) +COS($latitude* pi()/180) * COS(Latitude
* pi()/180) * POWER(SIN(( $longitude - Longitude) * pi()/180 / 2), 2)
))) as distance from mosque HAVING distance < 5 order by distance ";
$result = $connect->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$myArray = $result->fetch_all(MYSQLI_ASSOC);
print_r($myArray);
} else {
echo "0 results";
}
$myJSON = json_encode($myArray);
echo $myJSON;
$conn->close();
?>
这是我在swift中的代码:
import UIKit
class retriveM: NSObject,URLSessionDataDelegate {
var data : NSMutableData = NSMutableData()
func retrieveNearest(lat:Double,long:Double){
let url = NSMutableURLRequest(url: NSURL(string:
"http://mosqueksu.com/mosqueksu/retrieveNearest.php")! as URL )
url.httpMethod = "POST"
let postString = "latitude=\(lat),longitude=\(long)"
// Alamofire.request(.GET,url).responseJSON { response in
url.httpBody = postString.data(using: String.Encoding.utf8)
var session: URLSession!
let configuration = URLSessionConfiguration.default
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
let task = session.dataTask(with: url as URLRequest)
task.resume()
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)
{
self.data.append(data as Data);
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
{
if error != nil
{
print("Not Found", error as Any )
}
else
{
print("Ok")
self.parseJSON()
}
}
func parseJSON()
{
var jsonResult: NSArray = NSArray()
do
{
// jsonResult = try JSONSerialization.jsonObject(with: data as Data, options: .allowFragments) as! NSArray
jsonResult = try JSONSerialization.jsonObject(with: data as Data, options: .allowFragments) as! NSArray
print("jsonResult.count",jsonResult.count)
}
catch let error as NSError
{
print("jsonResult: ", error)
}
var jsonElement: NSDictionary = NSDictionary()
var contador = 0
for i in (0..<jsonResult.count)
{
jsonElement = jsonResult[i] as! NSDictionary
if let id = jsonElement["OBJECTID"] as? Int,
let name = jsonElement["ArabicName"] as? String,
let nameE = jsonElement["EnglishNam"] as? String,
let lat = jsonElement["Latitude"] as? Double,
let long = jsonElement["Longitude"] as? Double,
let status = jsonElement["status"] as? String,
let distance = jsonElement["distance"] as? Double
{
print("id: ", id)
print("name: ", name)
print("nameE: ", nameE)
print("latitude",lat)
print("longitude",long)
print ("status",status)
print ("distance",distance)
}
}
}
}
我想我从parseJson那里得到了这个错误我尝试了不同的方法,但它对我不起作用,感谢你的帮助。
答案 0 :(得分:0)
实际上这是Api的附带问题。即,响应既不是数组也不是字典,
,所以请让后端团队创建正确的json响应。
样本响应
{
"ReturnCode":"1",
"ReturnMsg":"Successfull Transaction",
"Data":{
"EmployeeName":"Admin",
"EmployeeID":1
}
}