我无法将对象数组转换为可以发送和存储到 Web 服务器的格式
I'm not able to convert the array of objects to format that can be send and store to web server
我正在尝试使用 urlsession 将对象数组发送到 Web 服务器。我需要将对象数组转换为某种格式,以便发送和存储到服务器中。
model.swift
class UserDetails
{
var name: String
var phNumber: Int
var pincode : Int
var numberOfItems: Int
init(name: String, number: Int, pin : Int, items: Int) {
self.name = name
self.phNumber = number
self.pincode = pin
self.numberOfItems = items
}
}
/* viewController.swift */
var users: [UserDetails] = []
func sendLabDetails(userId: Int,users: [UserDetails], completion: @escaping (_ serverResponse: NSDictionary) -> Void)
{
let parameters = ["user_id": userId, "users": users] as [String : Any]
}
我认为我在用对象调用 API 时做错了。请帮我提供一些关于如何将对象数组转换为字典数组或任何其他 JSON 支持的格式的代码。
如果您有 Dictionary
,您可以使用 JSONEncoder
:
将其转换为 JSON
let parameters = ["user_id": "newID", "users": "newUser"]
do{
let jsonParameter = try JSONEncoder().encode(parameters)
}catch{
print(error.localizedDescription)
}
NOTE: To encode an object means that this object conforms to protocol Codable
. Any
does not conform to this protocol, so it cannot be encoded as JSON.
In order to do this, you can make your class UserDetails
to conform that protocol or you have to change your data structure, because [String:Any]
is not good.
示例:
class UserDetails: Codable //Be sure to make the class to conform to this protocol if needed
{
var name: String
var phNumber: Int
var pincode : Int
var numberOfItems: Int
init(name: String, number: Int, pin : Int, items: Int) {
self.name = name
self.phNumber = number
self.pincode = pin
self.numberOfItems = items
}
}
//Now you can encode:
var users: [UserDetails] = []
func sendLabDetails(userId: Int,users: [UserDetails], completion: @escaping (_ serverResponse: NSDictionary) -> Void)
{
let parameters = ["user_id": userId, "users": users]
do{
let jsonParameter = try JSONEncoder().encode(parameters)
//HERE YOU CAN USE jsonParameter to send data to server
}catch{
print(error.localizedDescription)
}
}
我正在尝试使用 urlsession 将对象数组发送到 Web 服务器。我需要将对象数组转换为某种格式,以便发送和存储到服务器中。
model.swift
class UserDetails
{
var name: String
var phNumber: Int
var pincode : Int
var numberOfItems: Int
init(name: String, number: Int, pin : Int, items: Int) {
self.name = name
self.phNumber = number
self.pincode = pin
self.numberOfItems = items
}
}
/* viewController.swift */
var users: [UserDetails] = []
func sendLabDetails(userId: Int,users: [UserDetails], completion: @escaping (_ serverResponse: NSDictionary) -> Void)
{
let parameters = ["user_id": userId, "users": users] as [String : Any]
}
我认为我在用对象调用 API 时做错了。请帮我提供一些关于如何将对象数组转换为字典数组或任何其他 JSON 支持的格式的代码。
如果您有 Dictionary
,您可以使用 JSONEncoder
:
let parameters = ["user_id": "newID", "users": "newUser"]
do{
let jsonParameter = try JSONEncoder().encode(parameters)
}catch{
print(error.localizedDescription)
}
NOTE: To encode an object means that this object conforms to protocol
Codable
.Any
does not conform to this protocol, so it cannot be encoded as JSON. In order to do this, you can make your classUserDetails
to conform that protocol or you have to change your data structure, because[String:Any]
is not good.
示例:
class UserDetails: Codable //Be sure to make the class to conform to this protocol if needed
{
var name: String
var phNumber: Int
var pincode : Int
var numberOfItems: Int
init(name: String, number: Int, pin : Int, items: Int) {
self.name = name
self.phNumber = number
self.pincode = pin
self.numberOfItems = items
}
}
//Now you can encode:
var users: [UserDetails] = []
func sendLabDetails(userId: Int,users: [UserDetails], completion: @escaping (_ serverResponse: NSDictionary) -> Void)
{
let parameters = ["user_id": userId, "users": users]
do{
let jsonParameter = try JSONEncoder().encode(parameters)
//HERE YOU CAN USE jsonParameter to send data to server
}catch{
print(error.localizedDescription)
}
}