如何在swift4中为该json数据创建结构?
我创建了一个非常简单的json数据用于实践,但当JSONDecoder()解码时,它总是解码错误。我尝试了一些方法来修改我的结构,但都得到了相同的错误(打印“error0”)。代码如下所示如何在swift4中为该json数据创建结构?,swift,Swift,我创建了一个非常简单的json数据用于实践,但当JSONDecoder()解码时,它总是解码错误。我尝试了一些方法来修改我的结构,但都得到了相同的错误(打印“error0”)。代码如下所示 struct ss : Codable { var a : String var b : String } let js = "[{\"a\":\"1\",\"b\":\"2\"},{\"c\":\"3\",\"d\":\"4\"}]" let data = js.data(using:
struct ss : Codable {
var a : String
var b : String
}
let js = "[{\"a\":\"1\",\"b\":\"2\"},{\"c\":\"3\",\"d\":\"4\"}]"
let data = js.data(using: .utf8)
let a = [ss].self
do {
if let s = try? JSONDecoder().decode(a, from : data!) {
print(s[0].a)
}else{
print("error0")
}
}catch{
print("error1")
}
您的
JSON
replace有问题
let js = "[{\"a\":\"1\",\"b\":\"2\"},{\"c\":\"3\",\"d\":\"4\"}]"
与
另一个字典没有键a
和b
,这就是为什么jsondeconder
无法解码的原因,现在您的更新代码将是:
struct ss : Codable {
var a : String
var b : String
}
let js = "[{\"a\":\"1\",\"b\":\"2\"},{\"a\":\"3\",\"b\":\"4\"}]"
let data = js.data(using: .utf8)
let a = [ss].self
do {
let jsonDecoder = JSONDecoder()
let s = try jsonDecoder.decode(a, from: data!)
print(s[0].a) //"1\n"
} catch {
print(error)
}
PS:正如@Milander所建议的,如果您不想修复JSON
,您可以在Struct
中设置可选属性
struct ss : Codable {
let a, b, c, d: String?
}
您的JSON
replace有问题
let js = "[{\"a\":\"1\",\"b\":\"2\"},{\"c\":\"3\",\"d\":\"4\"}]"
与
另一个字典没有键a
和b
,这就是为什么jsondeconder
无法解码的原因,现在您的更新代码将是:
struct ss : Codable {
var a : String
var b : String
}
let js = "[{\"a\":\"1\",\"b\":\"2\"},{\"a\":\"3\",\"b\":\"4\"}]"
let data = js.data(using: .utf8)
let a = [ss].self
do {
let jsonDecoder = JSONDecoder()
let s = try jsonDecoder.decode(a, from: data!)
print(s[0].a) //"1\n"
} catch {
print(error)
}
PS:正如@Milander所建议的,如果您不想修复JSON
,您可以在Struct
中设置可选属性
struct ss : Codable {
let a, b, c, d: String?
}
您可以定义其他关键点,如下所示:
无可选
无替换
struct ss : Codable {
var a : String
var b : String
init(from decoder: Decoder) throws {
if let con = try? decoder.container(keyedBy: CodingKeys.self), let a = try? con.decode(String.self, forKey: .a), let b = try? con.decode(String.self, forKey: .b) {
self.a = a
self.b = b
} else if let con = try? decoder.container(keyedBy: AdditionalInfoKeys.self), let c = try? con.decode(String.self, forKey: .c), let d = try? con.decode(String.self, forKey: .d) {
a = c
b = d
} else {
throw NSError(domain: "Decoding error", code: 123, userInfo: nil)
}
}
enum AdditionalInfoKeys: String, CodingKey {
case c, d
}
}
您可以定义其他关键点,如下所示:
无可选
无替换
struct ss : Codable {
var a : String
var b : String
init(from decoder: Decoder) throws {
if let con = try? decoder.container(keyedBy: CodingKeys.self), let a = try? con.decode(String.self, forKey: .a), let b = try? con.decode(String.self, forKey: .b) {
self.a = a
self.b = b
} else if let con = try? decoder.container(keyedBy: AdditionalInfoKeys.self), let c = try? con.decode(String.self, forKey: .c), let d = try? con.decode(String.self, forKey: .d) {
a = c
b = d
} else {
throw NSError(domain: "Decoding error", code: 123, userInfo: nil)
}
}
enum AdditionalInfoKeys: String, CodingKey {
case c, d
}
}
抓住错误。有一种尝试你根本不听。并打印捕获的错误。它应该表示找不到键“a”的值。这是因为在第二个字典中,键是“c”和“d”,而不是“a”和“b”。Larme:我编辑代码,tksYour应该修复JSON。JSON数组中的第二项没有a和b属性。或者,您可以将属性设置为可选,并将为零。print(“error1”)
:否,请执行print(“error1:\(error)”)
。存在一个错误
对象,该对象可以包含信息、有用信息,并且可以不同。所以请阅读,不要只写“错误1”,抓住错误。有一种尝试你根本不听。并打印捕获的错误。它应该表示找不到键“a”的值。这是因为在第二个字典中,键是“c”和“d”,而不是“a”和“b”。Larme:我编辑代码,tksYour应该修复JSON。JSON数组中的第二项没有a和b属性。或者,您可以将属性设置为可选,并将为零。print(“error1”)
:否,请执行print(“error1:\(error)”)
。存在一个错误
对象,该对象可以包含信息、有用信息,并且可以不同。所以请阅读它,没有“错误1”只有。很高兴帮助你!!很高兴帮助你!!