Ios 在swift 4.0中获取JSON数据时出错
我的JSON看起来像这样,数据不是来自服务器:Ios 在swift 4.0中获取JSON数据时出错,ios,swift4,Ios,Swift4,我的JSON看起来像这样,数据不是来自服务器: [ { "emp_id": "1", "fname": "Shreya", "lname": "Shah", "email_id": "shreyashah@gmail.com", "password": "shreya123", "date_of_birth": "14/03/1995", "gender": "Female",
[
{
"emp_id": "1",
"fname": "Shreya",
"lname": "Shah",
"email_id": "shreyashah@gmail.com",
"password": "shreya123",
"date_of_birth": "14/03/1995",
"gender": "Female",
"street1": "Arbudgiri Society",
"street2": "Nr.Rambaug Road",
"city": "Ahmedabad",
"zipcode": "380005",
"country": "India",
"country_code": "+91",
"phone_no": "456544545",
"emp_img": "employeeImages/shreyashah@gmail.com_services-pic2.jpg",
"emp_desig": "PHP Developer",
"emp_skills": "html,php,css,jquery,javascript",
"emp_edu": "BCA,MCA",
"emp_exp": "3years",
"emp_notice_period": "30days",
"emp_lang": "english,hindi,gujarati"
},
{
"emp_id": "2",
"fname": "Harish",
"lname": "Verma",
"email_id": "harishverma@gmail.com",
"password": "harish123",
"date_of_birth": "22/07/1994",
"gender": "Female",
"street1": "Satyam Skyline",
"street2": "Nr.Sola Cross Roads",
"city": "Ahmedabad",
"zipcode": "380005",
"country": "India",
"country_code": "+91",
"phone_no": "964783214",
"emp_img": "employeeImages/harishverma@gmail.com_services-pic2.jpg",
"emp_desig": "iOS Team Lead",
"emp_skills": "objective-c,swift",
"emp_edu": "BCA,MCA",
"emp_exp": "3years",
"emp_notice_period": "30days",
"emp_lang": "english,hindi,gujarati"
}
]
我在VIEWCONTROLLER代码中的代码,其中我解码了json并在文件中使用它:
import UIKit
struct EmployeeDisplayData: Decodable {
let emp_img: String
let fname: String
let emp_desig: String
let emp_exp: String
let country: String
let emp_notice_period: String
}
class UserViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var mainTableView: UITableView!
let URL_GET_DATA = "http://172.16.1.22/Get-Employee-API/get-employee/"
var employeeArray = [EmployeeDisplayData]()
override func viewDidLoad() {
super.viewDidLoad()
self.mainTableView.delegate = self
self.mainTableView.dataSource = self
getEmployee()
// Do any additional setup after loading the view, typically from a nib.
// Do any additional setup after loading the view.
}
func getEmployee(){
let empURL = URL(string: "http://172.16.1.22/Get-Employee-API/get-employee/")
URLSession.shared.dataTask(with: empURL!) { (data, response, error) in
do
{
if error == nil
{
self.employeeArray = try JSONDecoder().decode([EmployeeDisplayData].self, from: data!)
for mainArr in self.employeeArray
{
DispatchQueue.main.async {
self.mainTableView.reloadData()
}
}
print("****Employee Data****\(self.employeeArray)")
}
}
catch
{
print("Error in get JSON Data Employee\(error)")
}
}.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return employeeArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:EMpTableViewCell = tableView.dequeueReusableCell(withIdentifier: "EMpTableViewCell") as! EMpTableViewCell
let empData = employeeArray[indexPath.row]
cell.lblOne.text = empData.fname
cell.lblTwo.text = empData.emp_desig
cell.lblThree.text = empData.emp_exp
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
}
有人能帮我解决这个错误吗?我不知道这个错误是什么意思。我在大多数地方都看到过相同类型的错误,并试图解决它,但服务器上仍然出现了相同的错误
我的邮递员
]我得到了同样的错误,我通过更改
contentType
和许多其他内容进行了大量搜索,但没有一个是可以修复的<代码>邮递员原始和预览选项卡响应让我知道确切的问题在哪里
未加工的
预览
发生此错误的原因是连接到MySQL响应的文本开头。因此,请通知您的开发人员,他将解决此问题,可能是解决错误的方法。我也遇到了同样的错误,我通过更改
contentType
和其他许多内容进行了大量搜索,但没有一个解决了此问题<代码>邮递员原始和预览选项卡响应让我知道确切的问题在哪里
未加工的
预览
发生此错误的原因是连接到MySQL响应的文本开头。因此,请告知您的开发人员,他将解决此问题,可能是解决错误的原因。我投票将此问题作为离题题题结束,因为问题是服务器的json响应不正确。@bbarnhart,您个人认为此问题离题,但根据我和其他一些用户的意见,这是一个有效的问题。我投票将此问题作为离题结束,因为问题是服务器的json响应不正确。@bbarnhart,你个人认为这个问题离题,但根据我和其他一些用户的意见,这是一个有效的问题。@AudreyRozario,如果这个答案能帮助你接受这个问题。你好,Kuldeep,你能帮我回答我的新问题吗你好,Kuldeep,你能帮我的朋友回答她的问题吗?这是链接-->>>@AudreyRozario,更新您的API回复。@AudreyRozario,在此帖子中:@AudreyRozario,如果这个答案有助于您接受这个问题。您好Kuldeep,您能帮我回答我的新问题吗?您好Kuldeep,您能帮我的朋友回答她的问题吗?这是链接-->>>@AudreyRozario,更新您的API回复。@AudreyRozario,在此帖子中: