Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获得200响应,但不显示json值_Json_Swift_Networking_Urlsession - Fatal编程技术网

获得200响应,但不显示json值

获得200响应,但不显示json值,json,swift,networking,urlsession,Json,Swift,Networking,Urlsession,我已经用“wordsapi”写了一个网络请求,我得到了一个200状态,但是我收到的响应不是json let wordsAPI = "https://wordsapiv1.p.mashape.com/words/money/synonyms" let wordsAPIEndPoint = NSURL(string: wordsAPI) let request = NSMutableURLRequest(url: wordsAPIEndPoint! as

我已经用“wordsapi”写了一个网络请求,我得到了一个200状态,但是我收到的响应不是json

    let wordsAPI = 
    "https://wordsapiv1.p.mashape.com/words/money/synonyms"
    let wordsAPIEndPoint = NSURL(string: wordsAPI)
    let request = NSMutableURLRequest(url: wordsAPIEndPoint! as 
     URL)

    request.setValue("mapkey", forHTTPHeaderField: "X-Mashape-Key")
    request.httpMethod = "GET"
    let session = URLSession.shared

    let synonymResponse = session.dataTask(with: request as 
     URLRequest) { (data, response, error) -> Void in
        if let resp = response as? HTTPURLResponse {
           print(resp)
        }
     }
     synonymResponse.resume()
我的代码生成并运行,我只需要json中的响应

    let wordsAPI = 
    "https://wordsapiv1.p.mashape.com/words/money/synonyms"
    let wordsAPIEndPoint = NSURL(string: wordsAPI)
    let request = NSMutableURLRequest(url: wordsAPIEndPoint! as 
     URL)

    request.setValue("mapkey", forHTTPHeaderField: "X-Mashape-Key")
    request.httpMethod = "GET"
    let session = URLSession.shared

    let synonymResponse = session.dataTask(with: request as 
     URLRequest) { (data, response, error) -> Void in
        if let resp = response as? HTTPURLResponse {
           print(resp)
        }
     }
     synonymResponse.resume()
//我收到的答复

    { Status Code: 200, Headers {
    "Accept-Ranges" =     (
    bytes
    );
    "CF-RAY" =     (
     "4d4d9aacabdccf5c-IAD"
    );
    Connection =     (
    "keep-alive"
    );
    "Content-Length" =     (
    30
    );
    "Content-Type" =     (
    "application/json; charset=utf-8"
     );Date =     (
    "Fri, 10 May 2019 17:24:00 GMT"
      );

我需要用json对我设置为端点的任何值的同义词进行响应。在本例中,是“money”

您需要在完成时使用包含接收到的json数据的
data
var

let wordsAPI = "https://wordsapiv1.p.mashape.com/words/money/synonyms"
let wordsAPIEndPoint = URL(string: wordsAPI)
var request = URLRequest(url: wordsAPIEndPoint!) 
request.setValue("mapkey", forHTTPHeaderField: "X-Mashape-Key")
request.httpMethod = "GET"

URLSession.shared.dataTask(with: request) { (data, response, error) -> Void in
    guard let data = data else { return }
    let res = try! JSONDecoder().decode(Root.self,from:data)
    print(res.synonyms)
}.resume()


您需要在完成时使用包含接收到的json数据的
data
var

let wordsAPI = "https://wordsapiv1.p.mashape.com/words/money/synonyms"
let wordsAPIEndPoint = URL(string: wordsAPI)
var request = URLRequest(url: wordsAPIEndPoint!) 
request.setValue("mapkey", forHTTPHeaderField: "X-Mashape-Key")
request.httpMethod = "GET"

URLSession.shared.dataTask(with: request) { (data, response, error) -> Void in
    guard let data = data else { return }
    let res = try! JSONDecoder().decode(Root.self,from:data)
    print(res.synonyms)
}.resume()


data
包含您的JSON数据。仅供参考-不要在Swift中使用
NSURL
NSMutableURLRequest
。使用
URL
URLRequest
。执行
String(数据:data!,编码:.utf8)
并附加value@rmaddy当我尝试将数据设置为时?HTTPURLResponse和PrimeRESP,我收到了一个错误“值”RESP被定义但从来没有使用过;考虑用布尔测试和“表达式”从“数据?”到“任何”隐含的强制替换。您需要用您尝试的代码更新您的问题。
data
包含您的JSON数据。仅供参考-不要在Swift中使用
NSURL
NSMutableURLRequest
。使用
URL
URLRequest
。执行
String(数据:data!,编码:.utf8)
并附加value@rmaddy当我尝试将数据设置为时?HTTPURLResponse和PrimeRESP,我收到了一个错误“值”RESP被定义但从未使用;考虑用布尔测试和“隐式强制从‘数据’到‘任何’的表达式替换”你需要用你的未曾的代码更新你的问题。是否有效<代码>https://docs.rapidapi.com/docs/keys 需要一个有效的键,所以在运行之前请先替换可选(“word\”:“money\”,“synonyms\”:[])是我现在收到的响应,但没有同义词列表。请尝试另一个词,看看它是否是有效的Json,只需去掉反斜杠,即可检索值。“实例方法”decode(\uquot:from:)'要求'Root'符合'Decodable',并且'Use of undeclared type'codable'”还确保此处的此键
request.setValue(“mapkey”,forHTTPHeaderField:“X-Mashape-key”)
有效???
https://docs.rapidapi.com/docs/keys 
需要一个有效的密钥,因此在运行可选(“word\:“money\”,“synonyms\”:[])之前请先替换是我现在收到的响应,但没有同义词列表。请尝试另一个单词,看看它是否是有效的Json,只需去掉反斜杠即可检索值。“实例方法”decode(:from:)要求“Root”符合“Decodable”,并且“使用未声明的类型”codable”