Ios 如何使用对象内部对象的可编码编码编码

Ios 如何使用对象内部对象的可编码编码编码,ios,swift,swift5,codable,jsonencoder,Ios,Swift,Swift5,Codable,Jsonencoder,我正在使用我的对象负载处理JSON负载。但我在对象内部编码对象时遇到了问题 我的有效载荷等级是这样的 class ConversationPayload :BaseObject { var title : String? = "" var messageDict: MessagePayload = MessagePayload() var participants: [Int32] = [] var type: String = "&

我正在使用我的对象负载处理JSON负载。但我在对象内部编码对象时遇到了问题

我的有效载荷等级是这样的

class ConversationPayload :BaseObject {
    var title : String? = ""
    var messageDict: MessagePayload = MessagePayload()
    var participants: [Int32] = []
    var type: String = ""
    
    enum CodingKeys: String, CodingKey {
        case title = "title"
        case messageDict = "message"
        case participants = "participants"
        case type = "type"
    }

    override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        if title != nil {
            try container.encode(title, forKey: .title)
        }
        try container.encode(messageDict, forKey: .messageDict)
        try container.encode(participants, forKey: .participants)
        try container.encode(type, forKey: .type)
    }
    
}

class MessagePayload: BaseObject {
    var body : String = ""
    var isVideocallInvite: Bool = false
    var attachmentsPayload: MessageAttachmentPayload? = nil
    
    enum CodingKeys: String, CodingKey {
        case body = "body"
        case isVideocallInvite = "is_videocall_invite"
        case attachmentsPayload = "attachment"
    }

    override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(body, forKey: .body)
        try container.encode(isVideocallInvite, forKey: .isVideocallInvite)
        if attachmentsPayload != nil {
            try container.encode(attachmentsPayload, forKey: .attachmentsPayload)
        }
        
    }
}

class MessageAttachmentPayload: BaseObject {
    var photo : String = ""
    var photoType : String = "jpg"
}
这个是什么

class BaseObject:Codable{}
我想要在json负载中得到的是这样的东西

{"message": {"body": "body_string", "is_videocall_invite": 1}, "participants" : [user-id], "type" : "converstation_type","title":"title"}

有人知道我的有效载荷等级有什么问题吗?在codable上还不太熟悉。提前谢谢。

有什么问题吗?我刚刚测试了你的类,它有一些小问题,但它符合codable,并且编码没有问题

在游乐场:

var conversation = ConversationPayload()
var message = MessagePayload()
message.body = "message body"
message.isVideocallInvite = true
var attachment = MessageAttachmentPayload()
attachment.photo = "attachment_file"
message.attachmentsPayload = attachment
conversation.messageDict = message
conversation.title = "Title"
conversation.participants = [1, 2, 3, 4]
conversation.type = "Conversation Type"

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try! encoder.encode(conversation)
print(String(data: data, encoding: .utf8)!)
输出如下所示:

{
  "participants" : [
    1,
    2,
    3,
    4
  ],
  "message" : {
    "is_videocall_invite" : true,
    "attachment" : {
      "photo" : "attachment_file",
      "photoType" : "jpg"
    },
    "body" : "message body"
  },
  "title" : "Title",
  "type" : "Conversation Type"
}
我在
MessageAttachmentPayload
类中添加了
encode
CodingKey
,对值进行编码


这不是你所期望的吗?

我不确定你在这里有什么限制,但我会简化所有这些。让JSON数据模型尽可能接近JSON

struct ConversationJsonModel: Codable {
    var title: String?
    var message: MessageJsonModel
    var participants: [Int]
    var type: String
}

struct MessageJsonModel: Codable {
    var body: String
    var is_videocall_invite: Int
    var attachment: AttachmentJsonModel?
}

struct AttachmentJsonModel: Codable {
    var photo: String
    var photo_type: String // <-- assuming photo_type is the JSON member name.
}
最后,对JSON进行编码

let conversationPayload = ConversationPayload()
let json = try? JSONEncoder().encode(conversationPayload.makeConversationJsonModel())

这允许在JSON表示和负载模型之间进行清晰的分离。例如,在JSON中,
是\u videocall\u invite
Int
(0或1);同时,在有效载荷模型中,
isVideocallInvite
是一个
Bool

我看到了错误。API需要一个整数(1,0)在is\u videocall\u invite上,所以我将它从bool改为int。感谢您的响应,我注意到is\u videocall\u invite需要int而不是boolean。
let conversationPayload = ConversationPayload()
let json = try? JSONEncoder().encode(conversationPayload.makeConversationJsonModel())