简体   繁体   中英

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
    }
}

And

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

I am having a json response as the following.

{
    "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"
        }
    ]
}

So I have written the code with the help of " https://app.quicktype.io/ ". But the code is not compiling as it gives the following error. "Type of expression is ambiguous without more context". Can anyone help me to sort out the issue please?

在此处输入图像描述

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"
        }
        ]
        }
 """
}

// inside a 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)
        }

// parser 'struct's

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM