我使用NodeJS创建了一个本地Web API ...当我尝试使用JSON序列化获取数据时,我得到了这样的JSON响应
(
{
"__v" = 0;
"_id" = 5b37dbd9a8da053fc8cfec9d;
address = karaikal;
dob = 27091995;
gender = male;
name = "Dhanush kumar";
password = hithere;
}
)
因此由于括号(()“,所以我无法从这些JSON输出中获取名称。请帮我。
谢谢。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://localhost:8000/api/products")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print("Error")
}else{
if let content = data
{
do{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print(myJson)
if let name = myJson["name"] as? NSArray
{
print(name)
}
}catch{
print("U GOT AN ERROR")
}
}
}
};task.resume()
}
}