无法使用从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."
]

热门问答

微信小程序什么情况下需要域名,什么情况下不需要?

最爱开车啦互联网的敏感者
推荐已采纳

1:不需要域名:使用微信门店小程序、微信小店小程序,以及静态的小程序(只有前端)

2:需要域名:小程序代码包限制在2M之内,好多东西都没法在代码包中加载,只能通过第三方web后台管理,后台是第三方的,假设是一个web网站,网站必须有服务器,就需要域名备案。

报考TCA可以同时在一天报考运维和架构的考试吗?

推荐

可以的,同一天最多支持报考两个科目,若您报考了两门科目考试,考试将以连考的形式在同一考场内进行。(例:14:00-15:30考云从业者,15:30-17:00考TCA运维)

腾讯云服务器上配置nginx访问对象存储,这样算内网还是外网访问呢?

Jinqn

腾讯 · 高级工程师 (已认证)

腾讯云COS前端开发
推荐

同园区会是内网,准确验证方法是,ping 对应存储桶域名,看看如果是 100. 10. 9. 172. 这样开头的内网 IP,就可以确定是内网访问,否则是外网访问。

关于云空间使用后是否需要再次交费的问题?

骑牛看晨曦love&peace~
推荐
如果是包年包月的服务器一次性付费后,访问服务器里搭建的网站,服务器不会有其他的流量费的,但是如果是后付费按量 按流量计费的服务器,会按每小时扣用户使用服务器资源的费用,产生流量也会按照官网价格计费的 。 请参考文档:https://cloud.tencent.com/produ...... 展开详请

IM会话类型可以扩展吗?

software微软、谷歌、腾讯、阿里、百度……
推荐
您好,IM SDK会话分为以下几种 1、C2C 会话,表示单聊情况自己与对方建立的对话,读取消息和发送消息都是通过会话完成。 2、群会话,表示群聊情况下,群内成员组成的会话,群会话内发送消息群成员都可接收到。 会话分组暂不支持,需要您自行处理UI,同步分组信息。... 展开详请

Tencent Cloud API 3.0 SDK for PHP 没有文件夹 vendor?

推荐

为了防止和composer冲突,所以github上特意设置了不导出这个目录。如需要获取源码,请使用git clone的方式获取,不要用github上的下载源码方式。

所属标签

扫码关注云+社区

领取腾讯云代金券