代码之家  ›  专栏  ›  技术社区  ›  J A S K I E R

缺少json数组swift

  •  -1
  • J A S K I E R  · 技术社区  · 8 年前

    有一个代码是由失眠生成的,它起作用:

    <预> <代码>导入基础 让标题=[ “X-AUTH-HEADER”:“FB2F56FDE81F21926FC0B5B74702F71DA9F152EFA7B55887A308984B71C9CAC7FD44DA05583AB06D57D29794F59FC475D9F2D132CBC6E29C421F11330A30A613”, “content type”:“应用程序/x-www-form-urlencoded” ] 让postdata=nsdata(数据:“”.data(使用:string.encoding.utf8)!) 让request=nsmutableurlrequest(url:nsurl(string:“http://rekrutacja.backendzs.pl/note/”)!作为URL, 缓存策略:.useProtocolCachePolicy, 超时间隔:10.0) request.httpmethod=“获取” request.allhttpHeaderFields=标题 request.httpbody=作为数据发布数据 let session=urlsession.shared let datatask=session.datatask(with:request as urlrequest,completionhandler:(data,response,error)->void in 如果(错误!=零){ 打印(错误) }其他{ 让httpResponse=响应为?httpurlResponse公司 打印(httpresponse) } }) 数据任务。继续()

    它工作,必须返回项数组:

    。[
    {
    “note_id”:14,
    “title”:“最后一个”,
    “content”:“可能”,
    “版本”:16,
    “已创建”:1530825998,
    “修改”:1530825998
    },请
    {
    “note_id”:13,
    “title”:“再次”,
    “content”:“再次”,
    “版本”:15,
    “已创建”:153082532,
    “修改”:1530825532
    },请
    {
    “note_id”:12,
    “title”:“zzz”,
    “content”:“zzzz”,
    “版本”:14,
    “已创建”:1530825484,
    “修改”:1530825484
    },请
    {
    “note_id”:11,
    “title”:“nowy tytu”,
    “content”:“4”,
    “版本”:13,
    “已创建”:1530825331,
    “修改”:1530825331
    },请
    {
    “note_id”:10,
    “title”:“测试5”,
    “content”:“测试”,
    “版本”:12,
    “创建时间”:1530825252,
    “修改”:1530825252
    },请
    {
    “note_id”:9,
    “title”:“标题7”,
    “content”:“1111”,
    “版本”:11,
    “创建时间”:1530825225,
    “修改”:1530825225
    },请
    {
    “note_id”:7,
    “title”:“测试”,
    “content”:“内容”,
    “版本”:9,
    “创建时间”:1530817192,
    “修改”:1530817192
    },请
    {
    “note_id”:6,
    “title”:“泰图”,
    “content”:“tre__”,
    “版本”:8,
    “已创建”:1530550847,
    “修改”:1530550847
    },请
    {
    “note_id”:5,
    “title”:“泰图”,
    “content”:“tre__”,
    “版本”:7,
    “已创建”:1530547099,
    “修改”:1530547099
    },请
    {
    “note_id”:3,
    “title”:“标题”,
    “content”:“内容”,
    “版本”:3,
    “已创建”:1530469102,
    “修改”:1530469102
    },请
    {
    “note_id”:2,
    “title”:“标题”,
    “content”:“内容”,
    “版本”:2,
    “已创建”:1530468369,
    “修改”:1530468369
    }
    ]
    

    但是,它没有向这个列表提供来自服务器的响应。仅显示常规数据。我已经测试并检查了与Alamoire、Swiftyjson、Swift.get请求相关的每一篇文章,但它们都不起作用。

    考虑到以下信息:

    1. 正在建立与服务器的连接(代码)
    2. 如果要启动生成的“shell”代码并运行“terminal” (Mac OS)它返回数组。
    3. 分析错误?如果是-如何提取此数据?
      1. 尝试过字典、Swiftyjson、nssession…但我确实做错了什么。我第一次和JSON打交道,所以我可以犯愚蠢的错误!

        感谢所有愿意提前帮忙的人!!!

        =

    它可以工作,必须返回项目数组:

    [
        {
            "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
        }
    ]
    

    但是,它没有向这个列表提供来自服务器的响应。仅显示常规数据。我已经测试并检查了与Alamoire、Swiftyjson、Swift.get请求相关的每一篇文章,但它们都不起作用。

    考虑到这些信息:

    1. 正在建立与服务器的连接(代码)
    2. 如果要启动生成的“shell”代码并运行“terminal” (Mac OS)它返回数组。
    3. 分析错误?如果是-如何提取此数据?

    尝试过字典、Swiftyjson、nssession…但我确实做错了什么。我第一次和JSON打交道,所以我可以犯愚蠢的错误!

    感谢所有愿意提前帮忙的人!!!

    1 回复  |  直到 8 年前
        1
  •  2
  •   Shehata Gamal    8 年前

    httpResponse Decodable

    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)
     }