我是 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/