无法获取Wordpress JSON

时间:2019-05-20 15:41:38

标签: json swift swift3

我在使用WordPress从站点恢复的JSON遇到问题,问题是当我尝试查询JSON时,查询不返回任何内容。

我尝试使用Alamofire恢复JSON,但是它也不起作用,我也不知道如何恢复使用WordPress返回我的网站的JSON

我尝试通过以下方式恢复JSON,但它不起作用,它不返回任何内容:

let urlString = URL(string:“ https://www.sitioWeb.org.mx/wp-json/wp/v2/posts?per_page=100&tags=(id)”)

    let request = URLRequest(url: urlString!)

    let task = URLSession.shared.dataTask(with: request){data, response, error in
        guard let data = data else{
            print("Solicitud fallida \(error!)")
            return
        }

        do{
            print("Recibimos respuesta")

            if let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: String]{
                DispatchQueue.main.async {
                    let titulo = json["rendered"]
                    let content = json["content"]
                    let excerpt = json["excerpt"]

                    print(json)
                    print(titulo!)
                    print(content!)
                    print(excerpt!)
                }
            }
        }catch let parseError {
            print("Error al parsear: \(parseError)")
            let responseString = String(data: data, encoding: .utf8)
            print("respuesta: \(responseString!)")
        }
    }
    task.resume()

我还通过以下方式尝试了Alamofire:

Alamofire.request(“ https://www.sitioWeb.org.mx/wp-json/wp/v2/posts?per_page=100&tags=(id)”)。responseJSON(completionHandler:{响应

        if let json = response.result.value as? JSON{
            print(json)
        }

    })

但是它仍然不起作用。

这是JSON具有的结构:

[{     “ id”:3438,     “ date”:“ 2019-04-01T06:02:50”,     “ date_gmt”:“ 2019-04-01T12:02:50”,     “ guid”:{       “ rendered”:“ https://sitioWeb.org.mx/?p=3438”     },     “ modified”:“ 2019-04-01T06:02:50”,     “ modified_gmt”:“ 2019-04-01T12:02:50”,     “ slug”:“ documento-2019”,     “ status”:“发布”,     “ type”:“ post”,     “ link”:“ https://sitioWeb.org.mx/documento-2019 /”,     “标题”:{       “渲染”:“ Documento 2019”     },     “内容”:{       “ rendered”:“ https://sitioWeb.org.mx/wp-content/uploads/2019/04/document.pdf \” class = \“ pdfemb-viewer \” style = \“ \” data-width = \ “ max \” data-height = \“ max \” data-mobile-width = \“ 500 \” data-scrollbar = \“ none \” data-download = \“ off \” data-tracking = \“ on \ “ data-newwindow = \” on \“ data-pagetextbox = \” off \“ data-scrolltotop = \” off \“ data-startzoom = \” 100 \“ data-startfpzoom = \” 100 \“ data-toolbar = \“ bottom \” data-toolbar-fixed = \“ off \”> document.pdf
\ n“,       “受保护”:false     },     “摘录”:{       “ rendered”:“”,       “受保护”:false     },     “作者”:1,     “ featured_media”:0,     “ comment_status”:“已关闭”,     “ ping_status”:“关闭”,     “粘性”:错误,     “ template”:“”,     “格式”:“标准”,     “元”:[],     “类别”:[       39     ],     “标签”:[       54       55     ],     “ _links”:{       “自我”:[         {           “ href”:“ https://sitioWeb.org.mx/wp-json/wp/v2/posts/3438”         }       ],      “收藏”:[         {           “ href”:“ https://sitioWeb.org.mx/wp-json/wp/v2/posts”         }       ],       “关于”: [         {           “ href”:“ https://sitioWeb.org.mx/wp-json/wp/v2/types/post”         }       ],       “作者”:[         {           “可嵌入”:是的,           “ href”:“ https://sitioWeb.org.mx/wp-json/wp/v2/users/1”         }       ],       “回复”:[         {           “可嵌入”:是的,           “ href”:“ https://sitioWeb.org.mx/wp-json/wp/v2/comments?post=3438”         }       ],       “版本历史”:[         {           “计数”:1           “ href”:“ https://sitioWeb.org.mx/wp-json/wp/v2/posts/3438/revisions”         }       ],       “前身版本”:[         {           “ id”:3440,           “ href”:“ https://sitioWeb.org.mx/wp-json/wp/v2/posts/3438/revisions/3440”         }       ],       “ wp:attachment”:[         {           “ href”:“ https://sitioWeb.org.mx/wp-json/wp/v2/media?parent=3438”         }       ],       “ wp:term”:[         {           “分类法”:“类别”,           “可嵌入”:是的,           “ href”:“ https://sitioWeb.org.mx/wp-json/wp/v2/categories?post=3438”         },         {           “ taxonomy”:“ post_tag”,           “可嵌入”:是的,           “ href”:“ https://sitioWeb.org.mx/wp-json/wp/v2/tags?post=3438”         }       ],       “咖喱”:[         {           “ name”:“ wp”,           “ href”:“ https://api.w.org/ {rel}”,           “ templated”:正确         }       ]     }}]

控制台不会在JSON上返回任何错误

enter image description here

1 个答案:

答案 0 :(得分:1)

正确的类型是[[String: Any]]

if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]]

要获取作者的URL,请像下面那样深入JSON:

if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
    if let links = json[0]["links"] as? [[String: Any]] {
        if let author = links[0]["author"]? as? [String: Any] {
            if let authorURL = author["href"] as? String {

            }
        }
    }
}