无法使用从 json 文件加载的数据初始化 json 对象
Cannot initialize json object with Data loaded from a json file
我正在尝试创建一个简单的应用程序,其中的数据是从 json 文件加载的。
我的 json 文件的名称是:
summary.json
它包含以下内容:
{
"name": "Octavio",
"lastName": "Rojas",
"email": "octavio.rojas@globant.com",
"phone": "5581723801",
"degree": "Bachelors degree in international business and trade",
"summary": "I just turned two years old in Globant on April 3rd 2019, I’ve been working with Disney for all this time in different areas of their parks app like resort reservations, profile and payment methods, geolocation services and itinerary information during my time here I’ve been working on sustainment, increasing code coverage, automation, analytics, developing tests, doing refactors and developing critical components for the frameworks I’ve been assigned to. I’ve worked with all kinds of computers since childhood, I’ve also participated in different activities related with computer science and information technologies, like database development, technical support, unix systems administration, web development, flash development and iOS development."
}
我正在尝试使用此功能阅读它:
func loadJson() {
do {
guard let url = Bundle.main.url(forResource: "summary", withExtension: "json") else {
return
}
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
let jsonData = try decoder.decode(Person.self, from: data)
} catch {
print(error)
}
}
我的结构如下所示:
struct Person: Encodable {
let name: String
let lastName: String
let email: String
let phone: String
let degree: String
let summary: String
}
但每次我尝试用
解码数据
let jsonData = try decoder.decode(Person.self, from: data)
我收到这个错误:
dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Badly formed object around character 88." UserInfo={NSDebugDescription=Badly formed object around character 88.})))
如果我没有尝试加载 json 文件,而是将它放在我的 class 中,如下所示:
let data = """
{
"name": "Octavio",
"lastName": "Rojas",
"email": "octavio.rojas@globant.com"
"phone": "5581723801"
"degree": "Bachelors degree in international business and trade"
"summary": "I just turned two years old in Globant on April 3rd 2019, I’ve been working with Disney for all this time in different areas of their parks app like resort reservations, profile and payment methods, geolocation services and itinerary information during my time here I’ve been working on sustainment, increasing code coverage, automation, analytics, developing tests, doing refactors and developing critical components for the frameworks I’ve been assigned to. I’ve worked with all kinds of computers since childhood, I’ve also participated in different activities related with computer science and information technologies, like database development, technical support, unix systems administration, web development, flash development and iOS development."
}""".data(using: .utf8)
然后它起作用了,为什么会这样,我做错了什么?
提前致谢。
您的 json 遗漏 ,
已在此处更正
{
"name": "Octavio",
"lastName": "Rojas",
"email": "octavio.rojas@globant.com",
"phone": "5581723801",
"degree": "Bachelors degree in international business and trade",
"summary": "I just turned two years old in Globant on April 3rd 2019, I’ve been working with Disney for all this time in different areas of their parks app like resort reservations, profile and payment methods, geolocation services and itinerary information during my time here I’ve been working on sustainment, increasing code coverage, automation, analytics, developing tests, doing refactors and developing critical components for the frameworks I’ve been assigned to. I’ve worked with all kinds of computers since childhood, I’ve also participated in different activities related with computer science and information technologies, like database development, technical support, unix systems administration, web development, flash development and iOS development."
}
struct Person: Codable {
let name, lastName, email, phone,degree, summary: String
}
let per = try! JSONDecoder().decode(Person.self, from: data)
print(per)
在 Swift 中嵌入 JSON 需要使用 [] 而不是 {}
[
"name": "Octavio",
"lastName": "Rojas",
"email": "octavio.rojas@globant.com",
"phone": "5581723801",
"degree": "Bachelors degree in international business and trade",
"summary": "I just turned two years old in Globant on April 3rd 2019, I’ve been working with Disney for all this time in different areas of their parks app like resort reservations, profile and payment methods, geolocation services and itinerary information during my time here I’ve been working on sustainment, increasing code coverage, automation, analytics, developing tests, doing refactors and developing critical components for the frameworks I’ve been assigned to. I’ve worked with all kinds of computers since childhood, I’ve also participated in different activities related with computer science and information technologies, like database development, technical support, unix systems administration, web development, flash development and iOS development."
]
我正在尝试创建一个简单的应用程序,其中的数据是从 json 文件加载的。
我的 json 文件的名称是:
summary.json
它包含以下内容:
{
"name": "Octavio",
"lastName": "Rojas",
"email": "octavio.rojas@globant.com",
"phone": "5581723801",
"degree": "Bachelors degree in international business and trade",
"summary": "I just turned two years old in Globant on April 3rd 2019, I’ve been working with Disney for all this time in different areas of their parks app like resort reservations, profile and payment methods, geolocation services and itinerary information during my time here I’ve been working on sustainment, increasing code coverage, automation, analytics, developing tests, doing refactors and developing critical components for the frameworks I’ve been assigned to. I’ve worked with all kinds of computers since childhood, I’ve also participated in different activities related with computer science and information technologies, like database development, technical support, unix systems administration, web development, flash development and iOS development."
}
我正在尝试使用此功能阅读它:
func loadJson() {
do {
guard let url = Bundle.main.url(forResource: "summary", withExtension: "json") else {
return
}
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
let jsonData = try decoder.decode(Person.self, from: data)
} catch {
print(error)
}
}
我的结构如下所示:
struct Person: Encodable {
let name: String
let lastName: String
let email: String
let phone: String
let degree: String
let summary: String
}
但每次我尝试用
解码数据let jsonData = try decoder.decode(Person.self, from: data)
我收到这个错误:
dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Badly formed object around character 88." UserInfo={NSDebugDescription=Badly formed object around character 88.})))
如果我没有尝试加载 json 文件,而是将它放在我的 class 中,如下所示:
let data = """
{
"name": "Octavio",
"lastName": "Rojas",
"email": "octavio.rojas@globant.com"
"phone": "5581723801"
"degree": "Bachelors degree in international business and trade"
"summary": "I just turned two years old in Globant on April 3rd 2019, I’ve been working with Disney for all this time in different areas of their parks app like resort reservations, profile and payment methods, geolocation services and itinerary information during my time here I’ve been working on sustainment, increasing code coverage, automation, analytics, developing tests, doing refactors and developing critical components for the frameworks I’ve been assigned to. I’ve worked with all kinds of computers since childhood, I’ve also participated in different activities related with computer science and information technologies, like database development, technical support, unix systems administration, web development, flash development and iOS development."
}""".data(using: .utf8)
然后它起作用了,为什么会这样,我做错了什么?
提前致谢。
您的 json 遗漏 ,
已在此处更正
{
"name": "Octavio",
"lastName": "Rojas",
"email": "octavio.rojas@globant.com",
"phone": "5581723801",
"degree": "Bachelors degree in international business and trade",
"summary": "I just turned two years old in Globant on April 3rd 2019, I’ve been working with Disney for all this time in different areas of their parks app like resort reservations, profile and payment methods, geolocation services and itinerary information during my time here I’ve been working on sustainment, increasing code coverage, automation, analytics, developing tests, doing refactors and developing critical components for the frameworks I’ve been assigned to. I’ve worked with all kinds of computers since childhood, I’ve also participated in different activities related with computer science and information technologies, like database development, technical support, unix systems administration, web development, flash development and iOS development."
}
struct Person: Codable {
let name, lastName, email, phone,degree, summary: String
}
let per = try! JSONDecoder().decode(Person.self, from: data)
print(per)
在 Swift 中嵌入 JSON 需要使用 [] 而不是 {}
[
"name": "Octavio",
"lastName": "Rojas",
"email": "octavio.rojas@globant.com",
"phone": "5581723801",
"degree": "Bachelors degree in international business and trade",
"summary": "I just turned two years old in Globant on April 3rd 2019, I’ve been working with Disney for all this time in different areas of their parks app like resort reservations, profile and payment methods, geolocation services and itinerary information during my time here I’ve been working on sustainment, increasing code coverage, automation, analytics, developing tests, doing refactors and developing critical components for the frameworks I’ve been assigned to. I’ve worked with all kinds of computers since childhood, I’ve also participated in different activities related with computer science and information technologies, like database development, technical support, unix systems administration, web development, flash development and iOS development."
]