簡體   English   中英

JSON 解析問題 swift + Codable

[英]JSON parsing problem with swift + Codable

struct FieldData: Codable {
    let atRESTfmStatus: Int?
    let atRESTfmResult, username, name, avatar: String?
    let rating, pe081NoSMS, pe085LastActive, pe090Position: String?
    let userid: String?

    enum CodingKeys: String, CodingKey {
        case atRESTfmStatus = "at.RESTfmStatus"
        case atRESTfmResult = "at.RESTfmResult"
        case username, name, avatar, rating
        case pe081NoSMS = "Pe081_NoSMS"
        case pe085LastActive = "Pe085_LastActive"
        case pe090Position = "Pe090_Position"
        case userid
    }
}

let jsonString = jsonData.data(using: .utf8)!
let decoder = JSONDecoder()
let parsedData = decoder.decode(FieldData.self, from: jsonString)
print(parsedData)

我有如下 json 響應。

{
    "response": {
        "scriptError": "0",
        "dataInfo": {
            "database": "Hemfix_web",
            "layout": "Result",
            "table": "Empty",
            "totalRecordCount": 1,
            "foundCount": 1,
            "returnedCount": 1
        },
        "data": [
            {
                "fieldData": {
                    "at.RESTfmStatus": 0,
                    "at.RESTfmResult": "0F4E29D9-FC50-604C-9255-30B9B03FBF01",
                    "username": "",
                    "name": "",
                    "avatar": "",
                    "rating": "",
                    "Pe081_NoSMS": "",
                    "Pe085_LastActive": "",
                    "Pe090_Position": "",
                    "userid": ""
                },
                "portalData": {},
                "recordId": "27",
                "modId": "1"
            }
        ]
    },
    "messages": [
        {
            "code": "0",
            "message": "OK"
        }
    ]
}

所以我在“ https://app.quicktype.io/ ”的幫助下編寫了代碼。 但是代碼沒有編譯,因為它給出了以下錯誤。 “沒有更多上下文的表達類型是模棱兩可的”。 任何人都可以幫我解決這個問題嗎?

在此處輸入圖像描述

public var item: String {
    return """
    {
        "response": {
        "scriptError": "0",
        "dataInfo": {
        "database": "Hemfix_web",
        "layout": "Result",
        "table": "Empty",
        "totalRecordCount": 1,
        "foundCount": 1,
        "returnedCount": 1
        },
        "data": [
        {
        "fieldData": {
        "at.RESTfmStatus": 0,
        "at.RESTfmResult": "0F4E29D9-FC50-604C-9255-30B9B03FBF01",
        "username": "",
        "name": "",
        "avatar": "",
        "rating": "",
        "Pe081_NoSMS": "",
        "Pe085_LastActive": "",
        "Pe090_Position": "",
        "userid": ""
        },
        "portalData": {},
        "recordId": "27",
        "modId": "1"
        }
        ]
        },
        "messages": [
        {
        "code": "0",
        "message": "OK"
        }
        ]
        }
 """
}

// 在 function 內部

do {
            let strinData = item.data(using: .utf8)!
            let jsonData = try JSONSerialization.jsonObject(with: strinData, options: .allowFragments)
            let jsonModelData = try JSONSerialization.data(withJSONObject: jsonData, options: [JSONSerialization.WritingOptions.prettyPrinted])
            let decoder = JSONDecoder()
            let model = try decoder.decode(SimpleResponse.self, from: jsonModelData)
            print(model.messages.count)
        } catch let error {
            print(error.localizedDescription)
        }

//解析器'結構'

public struct SimpleResponse: Codable {
    public let response: Response
    public let messages: [Message]

    public init(response: Response, messages: [Message]) {
        self.response = response
        self.messages = messages
    }
}

// MARK: - Message
public struct Message: Codable {
    public let code, message: String

}

// MARK: - Response
public struct Response: Codable {
    public let scriptError: String
    public let dataInfo: DataInfo
    public let data: [Datum]

}

// MARK: - Datum
public struct Datum: Codable {
    public let fieldData: FieldData
    public let portalData: PortalData
    public let recordId, modId: String

}

// MARK: - FieldData
public struct FieldData: Codable {
    public let atRESTfmStatus: Int
    public let atRESTfmResult, username, name, avatar: String
    public let rating, pe081NoSMS, pe085LastActive, pe090Position: String
    public let userid: String

    enum CodingKeys: String, CodingKey {
        case atRESTfmStatus = "at.RESTfmStatus"
        case atRESTfmResult = "at.RESTfmResult"
        case username, name, avatar, rating
        case pe081NoSMS = "Pe081_NoSMS"
        case pe085LastActive = "Pe085_LastActive"
        case pe090Position = "Pe090_Position"
        case userid
    }
}

// MARK: - PortalData
public struct PortalData: Codable {

}

// MARK: - DataInfo
public struct DataInfo: Codable {
    public let database, layout, table: String
    public let totalRecordCount, foundCount, returnedCount: Int

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM