【问题标题】:Issue with parsing @odata values using Swift使用 Swift 解析 @odata 值的问题
【发布时间】:2020-02-17 07:10:23
【问题描述】:

我的服务响应如下:

{
"@odata.context" = "https://xxx-d.www.com/odata/$metadata#Members";
"@odata.count" = 1;
 value = ({
        email = "Suamasu@xxx.com";
        memberType = User;
        name = "Suse";
        title = "Manager";
   });
}

我可以使用 Decodable swift 类型解码 value,但是我需要从响应中解码 "@odata.count" 值。

struct Members: Decodable {
    let value: [Member]
}

struct Member: Decodable {
    let name: String?
    let email: String
    let memberType: String?
    let title: String?
}

【问题讨论】:

  • 你可以在这里发布你的 JSON 响应吗?
  • @MohitKumar 回复已发布。我已经更新了型号信息。

标签: ios odata swift4 decodable


【解决方案1】:

您可以尝试为成员添加编码键吗?

struct Members: Decodable {
    let context: String
    let count: Int
    let value: [Member]

    enum CodingKeys: String, CodingKey {
        case context = "@odata.context"
        case count = "@odata.count"
        case value 
    }
}

更多信息:https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2020-06-13
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    相关资源
    最近更新 更多