Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Alamofire和SwiftyJSON,当我使用json[“email”时,它会返回null?_Ios_Alamofire_Swifty Json - Fatal编程技术网

Ios 使用Alamofire和SwiftyJSON,当我使用json[“email”时,它会返回null?

Ios 使用Alamofire和SwiftyJSON,当我使用json[“email”时,它会返回null?,ios,alamofire,swifty-json,Ios,Alamofire,Swifty Json,我导入了Alamofire和SwiftyJSON(最新版本),我只想尝试API调用 以下是我在viewDidLoad中的操作: url: String = "https://api.solvap.com" //this is not the real url of the api Alamofire.request(url, method: .get) .validate() .responseJSON { response in switc

我导入了Alamofire和SwiftyJSON(最新版本),我只想尝试API调用

以下是我在viewDidLoad中的操作:

 url: String = "https://api.solvap.com" //this is not the real url of the api

 Alamofire.request(url, method: .get)
        .validate()
        .responseJSON { response in
        switch response.result {
        case .success(let value):
            let json = JSON(value)
            //this print statement prints the whole JSON list
            print(json)
            //this print statement just return null for some weird reason
            print(json["name"])
        case .failure(let error):
            print(error)
        }
    }
Xcode中的控制台输出:

 [
   {
     "name" : "John",
     "surname" : "Doe",
     "password" : "54321",
     "job" : "Developer",
     "token" : "iw4lcsk7h8do3y6fuw5vvzefn"
   }
]
null

你有没有想过如何解决这个问题以及为什么会发生这种情况?

我想出来了。。如果API响应如下所示:

(
    {
        "name" : "John",
        "surname" : "Doe",
        "password" : "54321",
        "job" : "Developer",
        "token" : "iw4lcsk7h8do3y6fuw5vvzefn"
    }
)
{
    "name" : "John",
    "surname" : "Doe",
    "password" : "54321",
    "job" : "Developer",
    "token" : "iw4lcsk7h8do3y6fuw5vvzefn"
}
这意味着API响应是错误的,最好的方法是从后端修复响应,而不是试图解析来自错误API响应的数据

没有理由浪费时间试图解析来自错误API响应的数据

API的正确响应应如下所示:

(
    {
        "name" : "John",
        "surname" : "Doe",
        "password" : "54321",
        "job" : "Developer",
        "token" : "iw4lcsk7h8do3y6fuw5vvzefn"
    }
)
{
    "name" : "John",
    "surname" : "Doe",
    "password" : "54321",
    "job" : "Developer",
    "token" : "iw4lcsk7h8do3y6fuw5vvzefn"
}

解析后的JSON对象是一个字典数组。尝试打印
print(json.first?[“name”])
@vfn我从
print(json.first?[“name”])中得到错误
Type'json.\u元素(aka'(String,json)')没有下标成员解决方案:
Alamofire.request(url,method:.get.).validate().responseJSON{switch response.result中的response{case.success://从case.success(let value):let json=json(data:response.data!)如果let name=json[0][“name”]。string{//现在您已经打印了值(name)}case.failure(let error):print(error)}
我使用response.data解析数据,然后
let name=jason[0][“name”].string
所以我可以使用它!@pavlossnicolaou我会添加某种验证,以防json为空。类似于“如果”(json.count>0){//do stuff}”。如果出于任何原因json为空,您的应用程序将崩溃。