ios - 使用 Swift 将 JSON 或 CSV 数据导入 Firestore

标签 ios swift firebase

看来仍然无法将 JSON 或 CSV 文件直接导入 Firestore 数据库。许多建议是针对无法很好地转换为 Swift 的基于 JavaScript 的应用程序。有人有一个好的 Swift 解决方案来使用 JSON/CSV 将数据添加到 Firestore 数据库吗?

//example json
[
  {
    "name": "Stone Cove Marina Inc",
    "email": "NOT IN SAMPLE",
    "category": "Docks",
    "category2": "Marinas",
    "category3": "Dock Builders",
    "address": "134 Salt Pond Rd",
    "city": "Wakefield",
    "state": "RI",
    "zip": 2879,
    "phone": "(401) 783-8990",
    "website": "http://stonecovemarinari.com"
  },
  {
    "name": "Bluehaven Homes",
    "email": "NOT IN SAMPLE",
    "category": "General Contractors",
    "category2": "Home Builders",
    "category3": "",
    "address": "5701 Time Sq",
    "city": "Amarillo",
    "state": "TX",
    "zip": 79119,
    "phone": "(806) 452-2545",
    "website": "http://www.bluehavenhomes.com/"
  }
]

//here is the database structure
//collection is "businesses"; each "business" gets a document id; within each document id set the data

database.collection("businesses").document().setData(/*data here*/)

最佳答案

你可以试试

let str = """

      [
      {
        "name": "Stone Cove Marina Inc",
        "email": "NOT IN SAMPLE",
        "category": "Docks",
        "category2": "Marinas",
        "category3": "Dock Builders",
        "address": "134 Salt Pond Rd",
        "city": "Wakefield",
        "state": "RI",
        "zip": 2879,
        "phone": "(401) 783-8990",
        "website": "http://stonecovemarinari.com"
      },
      {
        "name": "Bluehaven Homes",
        "email": "NOT IN SAMPLE",
        "category": "General Contractors",
        "category2": "Home Builders",
        "category3": "",
        "address": "5701 Time Sq",
        "city": "Amarillo",
        "state": "TX",
        "zip": 79119,
        "phone": "(806) 452-2545",
        "website": "http://www.bluehavenhomes.com/"
      }
    ]

"""

do {
    let json = try JSONSerialization.jsonObject(with:str.data(using:.utf8)!, options: []) as! [[String: Any]]
    for var i in 0...json.count - 1
    {
      database.collection("businesses").document().setData(json[i])
    }
} catch  {
    print(error)
}

关于ios - 使用 Swift 将 JSON 或 CSV 数据导入 Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53550286/

相关文章:

ios - Segue 不会执行 Swift

ios - iOS 中如何让剪切 View 接收触摸事件?

Swift 3 核心数据 - NSExpression forFunction : "sum:" throws error ("could not cast dictionary to Day")

ios - 是否可以使用 swift 将 Ipod 库中的音乐保存到我的应用程序中?

ios - Swift:运行时:线程问题:MyApp.DownloadManager.progress.setter 中的数据竞争:Swift.Float at 0x7b1400209130

python - 为什么我的 Firebase Firestore 查询不在 order_by 中?

javascript - React Native Firebase 搜索 ListView

firebase - Flutter Firestore Geo 查询距我所在位置一定距离

ios - Xcode 11.2.1错误: Command CompileSwiftSources failed with a nonzero exit code

ios - 如何将 32 位二进制文​​件添加到 UIKit for Mac 上的 libsodium?