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

时间:2019-05-22 20:13:30

标签: json swift decodable

我正在尝试创建一个简单的应用程序,其中从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)

然后它起作用了,这是为什么,我在做什么错呢?

谢谢。

2 个答案:

答案 0 :(得分:1)

您的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)

答案 1 :(得分:0)

在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."
]