【问题标题】:Failed to get a Wordpress JSON无法获取 Wordpress JSON
【发布时间】:2019-10-07 00:24:49
【问题描述】:

我使用 WordPress 从网站恢复的 JSON 有问题,问题是当我尝试查询 JSON 时,我的查询没有返回任何内容。

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

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

让 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 的结构:

[{ “身份证”:3438, "日期": "2019-04-01T06:02:50", "date_gmt": "2019-04-01T12:02:50", “指导”:{ “渲染”:“https://sitioWeb.org.mx/?p=3438” }, “修改”:“2019-04-01T06:02:50”, “modified_gmt”:“2019-04-01T12:02:50”, “蛞蝓”:“documento-2019”, “状态”:“发布”, “类型”:“帖子”, "链接": "https://sitioWeb.org.mx/documento-2019/", “标题”: { “渲染”:“Documento 2019” }, “内容”: { “渲染”:“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", “受保护”:假 }, “摘录”:{ “渲染”:“”, “受保护”:假 }, “作者”:1, “特色媒体”:0, “comment_status”:“关闭”, “ping_status”:“关闭”, “粘性”:错误, “模板”: ””, “格式”:“标准”, “元”:[], “类别”:[ 39 ], “标签”:[ 54, 55 ], “_链接”:{ “自己”: [ { "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" } ], “前身版本”:[ { “身份证”:3440, "href": "https://sitioWeb.org.mx/wp-json/wp/v2/posts/3438/revisions/3440" } ], “wp:附件”:[ { "href": "https://sitioWeb.org.mx/wp-json/wp/v2/media?parent=3438" } ], “wp:术语”:[ { “分类”:“类别”, “可嵌入”:是的, "href": "https://sitioWeb.org.mx/wp-json/wp/v2/categories?post=3438" }, { “分类”:“post_tag”, “可嵌入”:是的, "href": "https://sitioWeb.org.mx/wp-json/wp/v2/tags?post=3438" } ], “居里”:[ { “名称”:“wp”, "href": "https://api.w.org/{rel}", “模板化”:真 } ] } }]

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

【问题讨论】:

  • 结果是[[String: Any]]注意方括号[]Any,因为值至少是IntStringDictionary
  • 是的,你是对的,现在我不明白如何从 json 中的某个标签中恢复数据

标签: json swift swift3


【解决方案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 {

            }
        }
    }
}

【讨论】:

  • 是的,它可以恢复我的 JSON,但现在我不明白如何获取 JSON 中标签的值,我的意思是如何恢复这个:author = ({href = "一些链接"})
  • , options: .allowFragments) 在这里毫无意义
  • @EnriqueEspinosa 这也应该可以,有什么问题
  • @Sh_Khan 这段代码给了我错误:,options: nil,错误是:“Nil is not compatible with expected argument typ 'JSONSerialization.ReadingOptions'”
  • 更新@EnriqueEspinosa
猜你喜欢
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-03
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
相关资源
最近更新 更多