未能获得 Wordpress JSON
Failed to get a Wordpress JSON
我从 WordPress 站点恢复的 JSON 有问题,问题是当我尝试查询 JSON 时,我的查询没有 return任何东西。
我尝试使用 Alamofire 来恢复我的 JSON,但它也不起作用,我不知道如何使用 WordPress[=] 来恢复 return 我网站的 JSON 29=]
我尝试通过以下方式恢复 JSON 但它不起作用,它没有 return 任何东西:
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: { response in
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": "publish",
"type": "post",
"link": "https://sitioWeb.org.mx/documento-2019 /",
"title":{
"rendered": "Documento 2019"
},
"content":{
"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",
"protected": 错误
},
"excerpt":{
"rendered": "",
"protected": 错误
},
"author": 1,
"featured_media": 0,
"comment_status": "closed",
"ping_status": "closed",
"sticky":错误,
"template": "",
"format": "standard",
"meta": [],
"categories": [
39
],
"tags": [
54、
55
],
“_链接”:{
"self": [
{
"href": "https://sitioWeb.org.mx/wp-json/wp/v2/posts/3438"
}
],
"collection": [
{
"href": "https://sitioWeb.org.mx/wp-json/wp/v2/posts"
}
],
"about":[
{
"href":“https://sitioWeb.org.mx/wp-json/wp/v2/types/post”
}
],
"author": [
{
"embeddable":是的,
"href":“https://sitioWeb.org.mx/wp-json/wp/v2/users/1”
}
],
"replies":[
{
"embeddable":是的,
"href": "https://sitioWeb.org.mx/wp-json/wp/v2/comments?post=3438"
}
],
"version-history": [
{
"count": 1,
"href": "https://sitioWeb.org.mx/wp-json/wp/v2/posts/3438/revisions"
}
],
"predecessor-version": [
{
"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": [
{
"taxonomy": "category",
"embeddable":是的,
"href":“https://sitioWeb.org.mx/wp-json/wp/v2/categories?post=3438”
},
{
"taxonomy": "post_tag",
"embeddable":是的,
"href": "https://sitioWeb.org.mx/wp-json/wp/v2/tags?post=3438"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": 是的
}
]
} }]
控制台 return JSON
上没有任何错误
正确的类型是[[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 {
}
}
}
}
我从 WordPress 站点恢复的 JSON 有问题,问题是当我尝试查询 JSON 时,我的查询没有 return任何东西。
我尝试使用 Alamofire 来恢复我的 JSON,但它也不起作用,我不知道如何使用 WordPress[=] 来恢复 return 我网站的 JSON 29=]
我尝试通过以下方式恢复 JSON 但它不起作用,它没有 return 任何东西:
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: { response in
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": "publish",
"type": "post",
"link": "https://sitioWeb.org.mx/documento-2019 /",
"title":{
"rendered": "Documento 2019"
},
"content":{
"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",
"protected": 错误
},
"excerpt":{
"rendered": "",
"protected": 错误
},
"author": 1,
"featured_media": 0,
"comment_status": "closed",
"ping_status": "closed",
"sticky":错误,
"template": "",
"format": "standard",
"meta": [],
"categories": [
39
],
"tags": [
54、
55
],
“_链接”:{
"self": [
{
"href": "https://sitioWeb.org.mx/wp-json/wp/v2/posts/3438"
}
],
"collection": [
{
"href": "https://sitioWeb.org.mx/wp-json/wp/v2/posts"
}
],
"about":[
{
"href":“https://sitioWeb.org.mx/wp-json/wp/v2/types/post”
}
],
"author": [
{
"embeddable":是的,
"href":“https://sitioWeb.org.mx/wp-json/wp/v2/users/1”
}
],
"replies":[
{
"embeddable":是的,
"href": "https://sitioWeb.org.mx/wp-json/wp/v2/comments?post=3438"
}
],
"version-history": [
{
"count": 1,
"href": "https://sitioWeb.org.mx/wp-json/wp/v2/posts/3438/revisions"
}
],
"predecessor-version": [
{
"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": [
{
"taxonomy": "category",
"embeddable":是的,
"href":“https://sitioWeb.org.mx/wp-json/wp/v2/categories?post=3438”
},
{
"taxonomy": "post_tag",
"embeddable":是的,
"href": "https://sitioWeb.org.mx/wp-json/wp/v2/tags?post=3438"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": 是的
}
]
} }]
控制台 return JSON
上没有任何错误正确的类型是[[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 {
}
}
}
}