我想获取WordPress页面的所有出版物,并且我使用以下插件。
这是 WP REST API 插件生成的 JSON 代码
[
{
"id": 65,
"date": "2014-08-24T18:56:26",
"date_gmt": "2014-08-24T18:56:26",
"guid": {
"rendered": "http:\/\/********\/********\/?p=1"
},
"modified": "2018-06-05T13:24:58",
"modified_gmt": "2018-06-05T13:24:58",
"slug": "this-url-wordpress",
"status": "publish",
"type": "post",
"title": {
"rendered": "\u2018 This a test title 1 \u2019"
},
"content": {
"rendered": "<p>This is a content 1</p>",
"protected": false
},
"excerpt": {
"rendered": "<p>this a excerpt 1...<\/p>\n",
"protected": false
},
"author": 1,
"featured_media": 468,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"template": "",
"format": "standard",
"meta": [
],
"categories": [
14
],
"tags": [
17,
18
],
},
{
"id": 650,
"date": "2014-08-24T18:56:26",
"date_gmt": "2014-08-24T18:56:26",
"guid": {
"rendered": "http:\/\/********\/********\/?p=1"
},
"modified": "2018-06-05T13:24:58",
"modified_gmt": "2018-06-05T13:24:58",
"slug": "this-url-wordpress",
"status": "publish",
"type": "post",
"title": {
"rendered": "\u2018 This a test title 2 \u2019"
},
"content": {
"rendered": "<p>This is a content 2</p>",
"protected": false
},
"excerpt": {
"rendered": "<p>this a excerpt 2...<\/p>\n",
"protected": false
},
"author": 1,
"featured_media": 468,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"template": "",
"format": "standard",
"meta": [
],
"categories": [
14
],
"tags": [
17,
18
],
},
{
"id": 230,
"date": "2014-08-24T18:56:26",
"date_gmt": "2014-08-24T18:56:26",
"guid": {
"rendered": "http:\/\/********\/********\/?p=1"
},
"modified": "2018-06-05T13:24:58",
"modified_gmt": "2018-06-05T13:24:58",
"slug": "this-url-wordpress",
"status": "publish",
"type": "post",
"title": {
"rendered": "\u2018 This a test title 3 \u2019"
},
"content": {
"rendered": "<p>This is a content 3</p>",
"protected": false
},
"excerpt": {
"rendered": "<p>this a excerpt 3...<\/p>\n",
"protected": false
},
"author": 1,
"featured_media": 468,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"template": "",
"format": "standard",
"meta": [
],
"categories": [
14
],
"tags": [
17,
18
],
},
]
有关 WP REST API 的更多信息: https://v2.wp-api.org
我的代码:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:peluqueriafran/WebView.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
Future<Post> fetchPost() async {
final response =
await http.get('http://**********:88/WordPress/wp-json/wp/v2/posts/');
final responseJson = json.decode(response.body);
return new Post.fromJson(responseJson);
}
class Post {
final int id;
final String title;
final String body;
final String urlimagen;
final String linkWeb;
Post({this.id, this.title, this.body, this.urlimagen, this.linkWeb});
factory Post.fromJson(Map<String, dynamic> json) {
return new Post(
title: json['title']['rendered'].toString(),
);
}
}
我的问题是: 如何用json选择所有出版物?
答案 0 :(得分:1)
在您的情况下,json.decode(response.body)
会返回一个List,而不是一个地图。
例如
解码JSON数组([])返回一个List。
解码JSON对象({})返回一个Map。
因此,无法将List转换为错误所说的Map。
要解决此问题,请执行以下操作: 由于外部列表只包含一个孩子,你应该这样做,
错了一个
return new Post.fromJson(responseJson);
正确的方式
return new Post.fromJson(responseJson[0]);