无法使用从json文件加载的数据初始化json对象

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (10)

我正在尝试创建一个简单的应用程序,从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文件,而是将它放在我的类中,如下所示:

    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)

然后它工作,为什么这是我做错了什么?

提前致谢。

提问于
用户回答回答于

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 ,在这里缺失了

{
    "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)

扫码关注云+社区

领取腾讯云代金券