【问题标题】:The data returned from the api is always nil?api返回的数据总是nil?
【发布时间】:2019-11-11 20:05:10
【问题描述】:

我正在从 API 检索数据。问题是数据总是返回为零。 它是一个问题列表,通常是 10 个。我编写的代码总是返回 10 个 nil 结果。我尝试使用具有相同结构的不同 API 的相同代码,它始终有效。

这是结构:

struct Questions: Codable{
    let question: String?
}

这是代码:

guard let url = URL(string: "http://adminsapi.somee.com/Api/Test/?id=1") else { return }
    URLSession.shared.dataTask(with: url) { (data, response, error) in
        guard let data = data else { return }
        do{
            let test = try? JSONDecoder().decode([Questions].self, from: data)
            for ask in test{
                print(ask.question)
            }
        }
    }.resume()

这是我得到的结果:

nil nil nil nil nil nil nil nil nil nil

这是postman中API返回的数据:

[
    {
        "AskName": "Urgenct of defecation"
    },
    {
        "AskName": "Mucous and streaked stools"
    },
    {
        "AskName": "vomiting"
    },
    {
        "AskName": "Fever"
    },
    {
        "AskName": "Tympany on percussion"
    },
    {
        "AskName": "Bowel sounds"
    },
    {
        "AskName": "shifting dullness"
    },
    {
        "AskName": "psoas and obturator sign"
    },
    {
        "AskName": "rebound tenderness"
    },
    {
        "AskName": "signs of shock"
    }
]

另外,当我尝试解析 json 并在一个字符串中读取整个数据时,它完全可以正常工作。我不知道问题出在哪里。

【问题讨论】:

  • 从不try? JSONDecoder()。添加 do - catch 块和 print error 实例。它准确地告诉你出了什么问题。提示:给定 URL 的数据不是 JSON。
  • @vadian 我认为他们在问题中使用了占位符 URL。问题中的 JSON,他们说他们从 Postman 收到的那个,是有效的 JSON。
  • @vadian 我尝试了 do - 捕获并打印错误,但它从不抛出任何错误
  • try后面的问号要去掉

标签: swift api httprequest urlsession


【解决方案1】:

您的Question 实现是错误的。您需要告诉编译器question 变量需要使用AskName 键进行解码。

struct Questions: Codable {
    let question: String?

    private enum CodingKeys: String, CodingKey {
        case question = "AskName"
    }
}

【讨论】:

    【解决方案2】:

    已编辑。您的问题结构不正确。这是正确解析结果的工作场代码。

    import UIKit
    import PlaygroundSupport
    
    let data = """
    [
        {
            "AskName": "Urgenct of defecation"
        },
        {
            "AskName": "Mucous and streaked stools"
        },
        {
            "AskName": "vomiting"
        },
        {
            "AskName": "Fever"
        },
        {
            "AskName": "Tympany on percussion"
        },
        {
            "AskName": "Bowel sounds"
        },
        {
            "AskName": "shifting dullness"
        },
        {
            "AskName": "psoas and obturator sign"
        },
        {
            "AskName": "rebound tenderness"
        },
        {
            "AskName": "signs of shock"
        }
    ]
    """.data(using: .utf8)!
    
    struct Questions: Codable{
        let AskName:String
    }
    
    do {
        try JSONDecoder().decode([Questions].self, from: data)
    } catch {
        print(error)
    }
    

    你也可以这样定义:

    struct Questions: Codable {
        let question: String
    
        private enum CodingKeys: String, CodingKey {
            case question = "AskName"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-05
      • 2017-03-28
      • 2015-03-05
      • 2014-08-25
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多