[英]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.我有如下 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"
}
]
}
So I have written the code with the help of " https://app.quicktype.io/ ".所以我在“ 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 // 在 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.