Flutter:如何使用JSON数据创建数组 [英] Flutter: How to Make an array with the JSON data

查看:883
本文介绍了Flutter:如何使用JSON数据创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用JSON制作数组,但是我重复了同一篇文章。
我正在为WordPress使用REST API JSON插件



有关 WP REST API 的详细信息:



应用:-选择了最后生成的,我想以获得所有帖子。


我希望有人能帮帮我,谢谢


解决方案

您可以更改 fetchPost 以返回帖子列表,例如:

  Future< List< Post>> fetchPosts()异步{
http.Response响应=
等待http.get('http:// **********:88 / WordPress / wp-json / wp / v2 / posts /');
列出responseJson = json.decode(response.body);
返回responseJson.map((m)=> new Post.fromJson(m))。toList();
}

然后可以使用 Future< List<像这样发布>

  @override 
Widget build(BuildContext context) {
return new FutureBuilder< List< Post>>(
future:fetchPosts(),
builder:(context,snapshot){
if(!snapshot.hasData)return Container();
List< Post> posts = snapshot.data;
返回新的ListView(
子代:posts.map((post)=> Text(post.title))。 toList(),
);
},
);
}

ListView 子级列表,就像 Column 等。因此,您可以使用包含子级列表的任何小部件


I would like to make an array with JSON, But I duplicate the same post. I am using the REST API JSON plugin for WordPress

More information about WP REST API: https://v2.wp-api.org

This is my JSON code

    [
  {
    "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
     ],
  },
]

My code:

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(),
      );
   }
}

WEB:

App: - The last generated is selected, I would like to get all the post.
I hope someone can help me out, thanks

解决方案

You could change fetchPost to return a List of Posts, like:

Future<List<Post>> fetchPosts() async {
  http.Response response =
      await http.get('http://**********:88/WordPress/wp-json/wp/v2/posts/');
  List responseJson = json.decode(response.body);
  return responseJson.map((m) => new Post.fromJson(m)).toList();
}

and you could then utilize the Future<List<Post>> like this:

@override
Widget build(BuildContext context) {
  return new FutureBuilder<List<Post>>(
    future: fetchPosts(),
    builder: (context, snapshot) {
      if (!snapshot.hasData) return Container();
      List<Post> posts = snapshot.data;
      return new ListView(
        children: posts.map((post) => Text(post.title)).toList(),
      );
    },
  );
}

ListView takes a List of children, just like Column etc. So you could use any of the Widgets that enclose a list of children

这篇关于Flutter:如何使用JSON数据创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆