ObjectMapper toJson 为空
ObjectMapper toJson empty
我正在尝试使用 ObjectMapper 将对象反序列化为 JSON 字典,但反序列化函数总是 return 空对象。
class TimeEntryContainer: Mappable {
//MARK: Properties
var entry: TimeEntryObject = TimeEntryObject()
//MARK: Initializers
init() {}
init(_ issue: Issue, hours: Double, activityId: Int) {
self.entry = TimeEntryObject(issue, hours: hours, activityId: activityId)
}
required init?(map: Map) {
mapping(map: map)
}
//MARK: Private Methods
func mapping(map: Map) {
entry <- map["time_entry"]
}
}
class TimeEntryObject {
//MARK: Properties
var issueId = -1
var projectId = ""
var hours = Double()
var activityId = -1
var comments = ""
//MARK: Initializers
init() {}
init(_ issue: Issue, hours: Double, activityId: Int) {
self.issueId = issue.id
self.projectId = issue.project
self.hours = hours
self.activityId = activityId
}
required init?(map: Map) {
mapping(map: map)
}
//MARK: Private functions
func mapping(map: Map) {
issueId <- map["issue_id"]
projectId <- map["project_id"]
hours <- map["hours"]
activityId <- map["activity_id"]
comments <- map["comments"]
}
}
这是我填充 TimeEntryContainer 对象的部分
let timeEntry = TimeEntryContainer()
timeEntry.entry.projectId = (issue?.project)!
timeEntry.entry.activityId = activityId
timeEntry.entry.hours = timeEntered
timeEntry.entry.comments = commentEdit.text ?? ""
let deserialized = Mapper().toJSONString(timeEntry)
print("hours: \(deserialized) ")
即使我的 timeEntry
对象的值设置正确,函数 Mapper().toJSONString()
、Mapper().toJSON()
甚至 timeEntry.toJSON()
和 timeEntry.toJSONString()
return 一个空的 JSON 对象/字典。我找不到哪里出错了
您的 TimeEntryObject 必须是可映射的。您输入了方法,但没有在 class 声明中声明一致性。
class TimeEntryObject: Mappable
我有同样的问题,在我的例子中,我实现了 Mappable 协议。 Biut 我没有映射 class 的变量,这给了我空 json.
发布只是另一种选择/解决方案。可以在需要时帮助某人。
我正在尝试使用 ObjectMapper 将对象反序列化为 JSON 字典,但反序列化函数总是 return 空对象。
class TimeEntryContainer: Mappable {
//MARK: Properties
var entry: TimeEntryObject = TimeEntryObject()
//MARK: Initializers
init() {}
init(_ issue: Issue, hours: Double, activityId: Int) {
self.entry = TimeEntryObject(issue, hours: hours, activityId: activityId)
}
required init?(map: Map) {
mapping(map: map)
}
//MARK: Private Methods
func mapping(map: Map) {
entry <- map["time_entry"]
}
}
class TimeEntryObject {
//MARK: Properties
var issueId = -1
var projectId = ""
var hours = Double()
var activityId = -1
var comments = ""
//MARK: Initializers
init() {}
init(_ issue: Issue, hours: Double, activityId: Int) {
self.issueId = issue.id
self.projectId = issue.project
self.hours = hours
self.activityId = activityId
}
required init?(map: Map) {
mapping(map: map)
}
//MARK: Private functions
func mapping(map: Map) {
issueId <- map["issue_id"]
projectId <- map["project_id"]
hours <- map["hours"]
activityId <- map["activity_id"]
comments <- map["comments"]
}
}
这是我填充 TimeEntryContainer 对象的部分
let timeEntry = TimeEntryContainer()
timeEntry.entry.projectId = (issue?.project)!
timeEntry.entry.activityId = activityId
timeEntry.entry.hours = timeEntered
timeEntry.entry.comments = commentEdit.text ?? ""
let deserialized = Mapper().toJSONString(timeEntry)
print("hours: \(deserialized) ")
即使我的 timeEntry
对象的值设置正确,函数 Mapper().toJSONString()
、Mapper().toJSON()
甚至 timeEntry.toJSON()
和 timeEntry.toJSONString()
return 一个空的 JSON 对象/字典。我找不到哪里出错了
您的 TimeEntryObject 必须是可映射的。您输入了方法,但没有在 class 声明中声明一致性。
class TimeEntryObject: Mappable
我有同样的问题,在我的例子中,我实现了 Mappable 协议。 Biut 我没有映射 class 的变量,这给了我空 json.
发布只是另一种选择/解决方案。可以在需要时帮助某人。