json - 在 swift 中创建 json 对象 : Consecutive statements on a line must be separated by ';'

标签 json swift xcode

我是 swift 的新手,并运行了一些有关创建 json 对象的演示,并且我正在尝试创建一个来测试我的一些类。从我看来,一切看起来都很好,但我是新的,所以可能缺少一些东西。

这是我正在使用的代码:

let contactRecords = [
        "listId" : 1,
        "phoneContacts": [{
            "phoneNumber": "264",
            "id": 1,
            "listID": 1,
            "orderID": 0,
            "description": "Call First During the Day",
            "dispatchDispositions": "",
            "viewFromWeb": false,
            "name": "Office",
            "uniqueId": 1,
            "readOnly": false,
            "changed": false
            },{
            "phoneNumber": "99999999",
            "id": 1,
            "listID": 1,
            "orderID": 0,
            "description": "",
            "dispatchDispositions": "",
            "viewFromWeb": false,
            "name": "Cell Phone",
            "uniqueId": 1,
            "readOnly": false,
            "changed": false
            }],
        
        ] as [String : Any]

我在 xcode 中遇到错误:一行中的连续语句必须用 ':' 分隔

我没有看到我做错了什么,它看起来像我能找到的每个演示中的 json 对象。大家看到了什么?

最佳答案

您混淆了字典和 JSON 字符串语法。它要么是一个不带任何大括号的 Swift 字典

let contactRecords : [String:Any] = [
    "listId" : 1,
    "phoneContacts": [[
        "phoneNumber": "264",
        "id": 1,
        "listID": 1,
        "orderID": 0,
        "description": "Call First During the Day",
        "dispatchDispositions": "",
        "viewFromWeb": false,
        "name": "Office",
        "uniqueId": 1,
        "readOnly": false,
        "changed": false
        ],[
        "phoneNumber": "99999999",
        "id": 1,
        "listID": 1,
        "orderID": 0,
        "description": "",
        "dispatchDispositions": "",
        "viewFromWeb": false,
        "name": "Cell Phone",
        "uniqueId": 1,
        "readOnly": false,
        "changed": false
        ]]
    ]

或带有大括号的文本 JSON 字符串

let contactRecords = 
"""
{   "listId" : 1,
    "phoneContacts": [{
        "phoneNumber": "264",
        "id": 1,
        "listID": 1,
        "orderID": 0,
        "description": "Call First During the Day",
        "dispatchDispositions": "",
        "viewFromWeb": false,
        "name": "Office",
        "uniqueId": 1,
        "readOnly": false,
        "changed": false
        },{
        "phoneNumber": "99999999",
        "id": 1,
        "listID": 1,
        "orderID": 0,
        "description": "",
        "dispatchDispositions": "",
        "viewFromWeb": false,
        "name": "Cell Phone",
        "uniqueId": 1,
        "readOnly": false,
        "changed": false
        }]
 }
 """

关于json - 在 swift 中创建 json 对象 : Consecutive statements on a line must be separated by ';' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63678265/

相关文章:

arrays - 如何按属性值(日期)对包含 NSManagedObject 子类实例的快速数组进行排序

ios - 解析 "orderByDescending"查询

ios - ld : framework not found Pods

iphone - 使后台工作在所有 iphone ios 上

cocoa - ShortcutRecorder记录CMD+Tab

json - node.js http.request 获取json,json前面未定义

json - Swift:解析 Json 字符串并将信息填充到字典中

c# - 将 JObject 转换为 Dictionary<string, object>。可能吗?

JavaScript:从 JSON 文件中获取 URL 并在后台执行

ios - 如何从 HealthKit 获取每日平均步数