使用数组格式读取JSON文件的内容失败

时间:2018-11-28 22:03:44

标签: json swift

我正在使用以下代码读取JSON文件的内容:

func loadJson(filename fileName: String) -> [categories]? {
    if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
        do {
            print("the url is",url.absoluteString)
            let data = try Data(contentsOf: url, options: .mappedIfSafe)
            print("the data content is \(data)")
            let decoder = JSONDecoder()
            let jsonData = try decoder.decode(ResponseData.self, from: data)
            return jsonData.person
        } catch {
            print("error:\(error)")
        }
    }
    return nil
}

文件内容如下:

 [
  {
    "name": "Telefonía",
    "icon": "mobile",
    "id": "5b4dd0a69a38237f574de754",
    "url": "https://es.trueketeke.io/anuncios-segunda-mano-categoria/telefonia/5b4dd0a69a38237f574de754",
    "children": [
      {
        "name": "Moviles",
        "id": "5b4dd0a69a38237f574de755",
        "categoryId": "5b4dd0a69a38237f574de754",
        "createdAt": "2018-07-17T11:19:02.231Z",
        "updatedAt": "2018-07-17T11:19:02.231Z",
        "nested": "Moviles",
        "children": [
          {
            "name": "Servicios de telefonía",
            "id": "5b4dd0a69a38237f574de756",
            "categoryId": "5b4dd0a69a38237f574de754",
            "subcategoryId": "5b4dd0a69a38237f574de755",
            "createdAt": "2018-07-17T11:19:02.327Z",
            "updatedAt": "2018-07-17T11:19:02.327Z",
            "nested": "Moviles > Servicios de telefonía",
            "subcategory": {
              "name": "Moviles",
              "id": "5b4dd0a69a38237f574de755",
              "categoryId": "5b4dd0a69a38237f574de754",
              "createdAt": "2018-07-17T11:19:02.231Z",
              "updatedAt": "2018-07-17T11:19:02.231Z",
              "nested": "Moviles"
            }
          },
          {
            "name": "Manos libres",
            "id": "5b4dd0a69a38237f574de757",
            "categoryId": "5b4dd0a69a38237f574de754",
            "subcategoryId": "5b4dd0a69a38237f574de755",
            "createdAt": "2018-07-17T11:19:02.416Z",
            "updatedAt": "2018-07-17T11:19:02.416Z",
            "nested": "Moviles > Manos libres",
            "subcategory": {
              "name": "Moviles",
              "id": "5b4dd0a69a38237f574de755",
              "categoryId": "5b4dd0a69a38237f574de754",
              "createdAt": "2018-07-17T11:19:02.231Z",
              "updatedAt": "2018-07-17T11:19:02.231Z",
              "nested": "Moviles"
            }
          },
          {
            "name": "Fundas",
            "id": "5b4dd0a69a38237f574de758",
            "categoryId": "5b4dd0a69a38237f574de754",
            "subcategoryId": "5b4dd0a69a38237f574de755",
            "createdAt": "2018-07-17T11:19:02.505Z",
            "updatedAt": "2018-07-17T11:19:02.505Z",
            "nested": "Moviles > Fundas",
            "subcategory": {
              "name": "Moviles",
              "id": "5b4dd0a69a38237f574de755",
              "categoryId": "5b4dd0a69a38237f574de754",
              "createdAt": "2018-07-17T11:19:02.231Z",
              "updatedAt": "2018-07-17T11:19:02.231Z",
              "nested": "Moviles"
            }
          },
          {
            "name": "Tarjetas prepago",
            "id": "5b4dd0a69a38237f574de759",
            "categoryId": "5b4dd0a69a38237f574de754",
            "subcategoryId": "5b4dd0a69a38237f574de755",
            "createdAt": "2018-07-17T11:19:02.606Z",
            "updatedAt": "2018-07-17T11:19:02.606Z",
            "nested": "Moviles > Tarjetas prepago",
            "subcategory": {
              "name": "Moviles",
              "id": "5b4dd0a69a38237f574de755",
              "categoryId": "5b4dd0a69a38237f574de754",
              "createdAt": "2018-07-17T11:19:02.231Z",
              "updatedAt": "2018-07-17T11:19:02.231Z",
              "nested": "Moviles"
            }
          },
          {
            "name": "Cables",
            "id": "5b4dd0a69a38237f574de75a",
            "categoryId": "5b4dd0a69a38237f574de754",
            "subcategoryId": "5b4dd0a69a38237f574de755",
            "createdAt": "2018-07-17T11:19:02.687Z",
            "updatedAt": "2018-07-17T11:19:02.687Z",
            "nested": "Moviles > Cables",
            "subcategory": {
              "name": "Moviles",
              "id": "5b4dd0a69a38237f574de755",
              "categoryId": "5b4dd0a69a38237f574de754",
              "createdAt": "2018-07-17T11:19:02.231Z",
              "updatedAt": "2018-07-17T11:19:02.231Z",
              "nested": "Moviles"
            }
          },
          {
            "name": "Cargadores",
            "id": "5b4dd0a69a38237f574de75b",
            "categoryId": "5b4dd0a69a38237f574de754",
            "subcategoryId": "5b4dd0a69a38237f574de755",
            "createdAt": "2018-07-17T11:19:02.784Z",
            "updatedAt": "2018-07-17T11:19:02.784Z",
            "nested": "Moviles > Cargadores",
            "subcategory": {
              "name": "Moviles",
              "id": "5b4dd0a69a38237f574de755",
              "categoryId": "5b4dd0a69a38237f574de754",
              "createdAt": "2018-07-17T11:19:02.231Z",
              "updatedAt": "2018-07-17T11:19:02.231Z",
              "nested": "Moviles"
            }
          },
          {
            "name": "Baterias",
            "id": "5b4dd0a69a38237f574de75c",
            "categoryId": "5b4dd0a69a38237f574de754",
            "subcategoryId": "5b4dd0a69a38237f574de755",
            "createdAt": "2018-07-17T11:19:02.872Z",
            "updatedAt": "2018-07-17T11:19:02.872Z",
            "nested": "Moviles > Baterias",
            "subcategory": {
              "name": "Moviles",
              "id": "5b4dd0a69a38237f574de755",
              "categoryId": "5b4dd0a69a38237f574de754",
              "createdAt": "2018-07-17T11:19:02.231Z",
              "updatedAt": "2018-07-17T11:19:02.231Z",
              "nested": "Moviles"
            }
          }
        ]
      }
     ]

JSON文件的内容远大于此内容。我切断了可读性。问题是读取失败:

  

错误:typeMismatch(Swift.Dictionary ,Swift.DecodingError.Context(codingPath:[],debugDescription:“预期对Dictionary 进行解码,但找到了一个数组。”,底层错误:无))

我猜想数据是一个字典,但是由于是数组,所以它失败了。怎么读?

1 个答案:

答案 0 :(得分:0)

替换

let jsonData = try decoder.decode(ResponseData.self, from: data)

使用

let jsonData = try decoder.decode([ResponseData].self, from: data)

因为您的根是数组[]而不是字典{}