Ios 缺少JSON数组SWIFT

Ios 缺少JSON数组SWIFT,ios,json,swift,get,swift4,Ios,Json,Swift,Get,Swift4,失眠产生了一个代码,它可以工作: import Foundation let headers = [ "x-auth-header": "fb2f56fde81f21926fc0b5b74702f71da9f152efa7b5587a308984b71c9acac7fd44da05583ab06d57d29794f59fc475d9f2d132cbc6e29c421f11330a30a613", "content-type": "application/x-www-form-urlen

失眠产生了一个代码,它可以工作:

import Foundation

let headers = [
  "x-auth-header": "fb2f56fde81f21926fc0b5b74702f71da9f152efa7b5587a308984b71c9acac7fd44da05583ab06d57d29794f59fc475d9f2d132cbc6e29c421f11330a30a613",
  "content-type": "application/x-www-form-urlencoded"
]

let postData = NSData(data: "".data(using: String.Encoding.utf8)!)

let request = NSMutableURLRequest(url: NSURL(string: "http://rekrutacja.backendzs.pl/note/")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
它可以工作,并且必须返回项目数组:

[
    {
        "note_id": 14,
        "title": "last one",
        "content": "maybe",
        "version": 16,
        "created": 1530825998,
        "modified": 1530825998
    },
    {
        "note_id": 13,
        "title": "again",
        "content": "again",
        "version": 15,
        "created": 1530825532,
        "modified": 1530825532
    },
    {
        "note_id": 12,
        "title": "zzz",
        "content": "zzzz",
        "version": 14,
        "created": 1530825484,
        "modified": 1530825484
    },
    {
        "note_id": 11,
        "title": "Nowy tytuł",
        "content": "4",
        "version": 13,
        "created": 1530825331,
        "modified": 1530825331
    },
    {
        "note_id": 10,
        "title": "test5",
        "content": "test",
        "version": 12,
        "created": 1530825252,
        "modified": 1530825252
    },
    {
        "note_id": 9,
        "title": "title7",
        "content": "1111",
        "version": 11,
        "created": 1530825225,
        "modified": 1530825225
    },
    {
        "note_id": 7,
        "title": "test",
        "content": "content",
        "version": 9,
        "created": 1530817192,
        "modified": 1530817192
    },
    {
        "note_id": 6,
        "title": "Tytuł",
        "content": "Treść",
        "version": 8,
        "created": 1530550847,
        "modified": 1530550847
    },
    {
        "note_id": 5,
        "title": "Tytuł",
        "content": "Treść",
        "version": 7,
        "created": 1530547099,
        "modified": 1530547099
    },
    {
        "note_id": 3,
        "title": "title",
        "content": "content",
        "version": 3,
        "created": 1530469102,
        "modified": 1530469102
    },
    {
        "note_id": 2,
        "title": "title",
        "content": "content",
        "version": 2,
        "created": 1530468369,
        "modified": 1530468369
    }
]
但是,它不提供此列表以及来自服务器的响应。仅显示常规数据。我已经测试并检查了与Alamofire、swiftyJSON、Swift.get请求相关的所有文章,但都不起作用

考虑到这一信息:

  • 正在建立与服务器的连接(代码)
  • 如果要启动生成的“外壳”代码并运行“终端” (Mac OS)它返回数组
  • 不正确的解析?如果是-如何提取此数据
  • 试用过的字典、快捷JSON、NSSession。。。但我肯定做错了什么。我第一次使用的是JSON,所以我会犯一些愚蠢的错误

    感谢每一个愿意提前帮助你的人


    如果在
    httpResponse
    中打印内容,则需要使用
    可解码返回的数据

    struct Item : Decodable {
    
     let title:String,content:String,version:Int,created:Int32,modified:Int32,noteId:Int
    
      private enum CodingKeys : String, CodingKey {
        case title,content,version,created,modified,noteId = "note_id"
      }
    
    
    }
    
    //

    内部响应完成

    do {
           let items = try JSONDecoder().decode([Item].self, from: data)
           print(items)
    }
    catch {
        print(error)
     }
    

    类型“Item”不符合类之前结构声明的协议“Decodable”,但它有一些错误。\ux。已经对此进行了搜索,谢谢)也许它应该只是带有参数“Decodable”的“struct”或“extension”?由于苹果的文档,它现在可以工作了,没有逗号。伙计!让上帝保佑你!))你已经在5分钟内解决了我几天来一直试图解决的问题。衷心感谢你!快乐编码=D