更新结构中的特定值
Update specific value in structure
我似乎无法找到如何更新结构中的特定值 Swift 4. 我有这个结构:
struct Export: Decodable {
let id: String
let name: String
let exportType: String
}
它充满了我从 JSON.
中得到的值
我正在使用 JSON解码器
self.Exp = try JSONDecoder().decode([Export].self, from: data!)
现在我收到一个新的 JSON 只包含一个 ID。
我想用新值更新这个结构的 id。
JSON 发送这样的响应:
{
"id": "70CD044D290945BF82F13C13B183F669"
}
所以即使我尝试将它保存在单独的结构中,我也会收到此错误
dataCorrupted(Swift.DecodingError.Context(codingPath: [],
debugDescription: "The given data was not valid JSON.",
underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840
"JSON text did not start with array or object and option to allow fragments not set."
UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.})))
我试图在发布之前寻找解决方案,但我找不到任何我对 JSON 处理和 Swift...
很陌生
首先, 你所有的 JSON 格式不正确。
其次, 在你收到正确的 JSON 之后,对于 self.Exp
你得到的是数组,但是对于你的 idDict
你只有一个字典对象。
所以,保留那些 属性 optional
,它们不需要出现在 JSON 中。在您的情况下,它将 name
和 exportType
为:
struct Export: Decodable {
var id: String
var name: String?
var exportType: String?
}
它可以用于 self.Exp
为:
self.Exp = try JSONDecoder().decode([Export].self, from: data!)
并且 idDict
为:,
idDict = try JSONDecoder().decode(Export.self, from: data!)
将 JSON 部分放在一边,您将无法更新 Export
中的 id
,因为它是一个 let
常量。您可能希望将其更改为 var
.
如果我没理解错的话,您收到的 JSON 响应只有 个 ID。您不会从中创建 Export
结构。您需要单独处理此 JSON 响应以获取您要查找的 ID。像这样:
import Foundation
let jsonText = """
{"id": "70CD044D290945BF82F13C13B183F669"}
"""
struct IdResponse: Codable {
let id: String
}
let idResponse: IdResponse = try! JSONDecoder().decode(IdResponse.self, from: jsonText.data(using: .utf8)!)
最后,更新您的 Export
结构:
import Foundation
struct Export: Decodable {
var id: String
let name: String
let exportType: String
}
// Build export object
var export: Export = Export(id: "1", name: "Name", exportType: "TypeA")
// Grab JSON response from somewhere, which contains an updated id
let idResponse: IdResponse = try! JSONDecoder().decode(IdResponse.self, from: jsonText.data(using: .utf8)!)
// Update the object
export.id = idResponse.id
我似乎无法找到如何更新结构中的特定值 Swift 4. 我有这个结构:
struct Export: Decodable {
let id: String
let name: String
let exportType: String
}
它充满了我从 JSON.
中得到的值
我正在使用 JSON解码器
self.Exp = try JSONDecoder().decode([Export].self, from: data!)
现在我收到一个新的 JSON 只包含一个 ID。
我想用新值更新这个结构的 id。
JSON 发送这样的响应:
{
"id": "70CD044D290945BF82F13C13B183F669"
}
所以即使我尝试将它保存在单独的结构中,我也会收到此错误
dataCorrupted(Swift.DecodingError.Context(codingPath: [],
debugDescription: "The given data was not valid JSON.",
underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840
"JSON text did not start with array or object and option to allow fragments not set."
UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.})))
我试图在发布之前寻找解决方案,但我找不到任何我对 JSON 处理和 Swift...
很陌生首先, 你所有的 JSON 格式不正确。
其次, 在你收到正确的 JSON 之后,对于 self.Exp
你得到的是数组,但是对于你的 idDict
你只有一个字典对象。
所以,保留那些 属性 optional
,它们不需要出现在 JSON 中。在您的情况下,它将 name
和 exportType
为:
struct Export: Decodable {
var id: String
var name: String?
var exportType: String?
}
它可以用于 self.Exp
为:
self.Exp = try JSONDecoder().decode([Export].self, from: data!)
并且 idDict
为:,
idDict = try JSONDecoder().decode(Export.self, from: data!)
将 JSON 部分放在一边,您将无法更新 Export
中的 id
,因为它是一个 let
常量。您可能希望将其更改为 var
.
如果我没理解错的话,您收到的 JSON 响应只有 个 ID。您不会从中创建 Export
结构。您需要单独处理此 JSON 响应以获取您要查找的 ID。像这样:
import Foundation
let jsonText = """
{"id": "70CD044D290945BF82F13C13B183F669"}
"""
struct IdResponse: Codable {
let id: String
}
let idResponse: IdResponse = try! JSONDecoder().decode(IdResponse.self, from: jsonText.data(using: .utf8)!)
最后,更新您的 Export
结构:
import Foundation
struct Export: Decodable {
var id: String
let name: String
let exportType: String
}
// Build export object
var export: Export = Export(id: "1", name: "Name", exportType: "TypeA")
// Grab JSON response from somewhere, which contains an updated id
let idResponse: IdResponse = try! JSONDecoder().decode(IdResponse.self, from: jsonText.data(using: .utf8)!)
// Update the object
export.id = idResponse.id