arrays - Swift 中的字典数组

标签 arrays json dictionary swift

似乎找不到正确的语法。尝试从 json 文件中获取一组字典:

let books:Array = jsonDict["books"] as Array
//Cannot convert the expression's type Array<$T4> to type StringLiteralConvertible

let books:Array = jsonDict["books"] as Array<Dictionary>
//Reference to generic type "Dicionary" requires arguments in <...>

json 看起来像这样:

{
  "id": "1",
  "title": "title one",
  "books": [
    {
      "id": "1",
      "title": "book title one"
    },
    {
      "id": "2",
      "title": "book title two"
    }
    ]
}

最佳答案

当您指定一个数组时,您还必须具体并列出数组中的内容。你还需要

您可能正在寻找的是:

let books = jsonDict["books"]

编译器应该能够推断出其中的内容。如果没有,您可以使用:

let books = jsonDict["books"] as Array<Dictionary<String, String>>

不需要类型转换书籍的类型说明符。

关于arrays - Swift 中的字典数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24541726/

相关文章:

c# - 为什么从字典中获取时会得到 Action<> 的克隆?

c# - JSON.NET 阅读器问题

arrays - MongoDB $addToSet 替换

arrays - 模糊 Vlookup 显示单行中的所有匹配结果

arrays - 为什么我的数组在发送到函数后是空的

javascript - 获取 json 文件时出错

json - 如何为 Firestore 创建大量示例数据?

javascript - 重新加载组件的 React.JS 问题

python - 如何从 python 字典列表中创建范围?

c++ - 如何打印我在 map 中使用的数组?