颤振解析复杂Json

颤振解析复杂Json,json,flutter,Json,Flutter,我在从API解析JSON时遇到了一些困难。 以前,JSON格式更简单: { "author": { "id": 4, "username": "maria" }, "description": "dsada", "

我在从API解析JSON时遇到了一些困难。 以前,JSON格式更简单:

        {   "author": {
                "id": 4,
                "username": "maria"
             },
            "description": "dsada",
            "image": "/media/posts/image_cropper_1616320457204.jpg",
            "created_at": "2021-03-21T09:54:38.645596Z"
        },
        {
            "author": {
                "id": 4,
                "username": "maria"
            },
            "description": "asdsad",
            "image": "/media/posts/image_cropper_1616070403137.jpg",
            "created_at": "2021-03-18T12:26:56.774797Z"
        }
我的JSON格式模型如下

class Feed {
  final String author;
  final String description;
  final String imageUrl;
  final String createdAt;

  Feed({this.author, this.description, this.imageUrl, this.createdAt});

  factory Feed.fromJson(Map<String, dynamic> json) {
    return Feed(
      author: json['author']['username'],
      description: json['description'],
      imageUrl: json['image'],
      createdAt: json['created_at'],
    );
  }
}


我不认为添加分页后JSON格式会改变。如果你对此有任何想法,请帮忙。谢谢。

要快速创建类,您可以使用此应用程序在开始时提供帮助,然后在必要时进行修改。()

类用户{
使用者({
这个,伯爵,
这个,下一个,,
这个,以前,,
这个结果,,
});
最终整数计数;
下一个是最终字符串;
最终动态上一步;
最终结果清单;
工厂用户.fromJson(映射json)=>User(
count:json[“count”],
next:json[“next”],
previous:json[“previous”],
结果:List.from(json[“results”].map((x)=>Result.fromJson(x)),
);
映射到JSON()=>{
“计数”:计数,
“下一个”:下一个,
“以前的”:以前的,
“results”:List.from(results.map((x)=>x.toJson()),
};
}
班级成绩{
结果({
这位作者,
这个.说明,,
这个,这个形象,,
这个.createdAt,
});
最终作者;
最终字符串描述;
最终字符串图像;
最终日期时间创建日期;
工厂结果.fromJson(映射json)=>Result(
作者:author.fromJson(json[“author”]),
description:json[“description”],
image:json[“image”],
createdAt:DateTime.parse(json[“created_at”]),
);
映射到JSON()=>{
“author”:author.toJson(),
“描述”:描述,
“形象”:形象,
“created_at”:createdAt.toiso8601字符串(),
};
}
类作者{
作者({
这个身份证,
这个.username,
});
最终int id;
最终字符串用户名;
factory Author.fromJson(映射json)=>Author(
id:json[“id”],
用户名:json[“用户名”],
);
映射到JSON()=>{
“id”:id,
“用户名”:用户名,
};
}

要快速创建类,您可以使用此应用程序在开始时提供帮助,然后在必要时进行修改。()

类用户{
使用者({
这个,伯爵,
这个,下一个,,
这个,以前,,
这个结果,,
});
最终整数计数;
下一个是最终字符串;
最终动态上一步;
最终结果清单;
工厂用户.fromJson(映射json)=>User(
count:json[“count”],
next:json[“next”],
previous:json[“previous”],
结果:List.from(json[“results”].map((x)=>Result.fromJson(x)),
);
映射到JSON()=>{
“计数”:计数,
“下一个”:下一个,
“以前的”:以前的,
“results”:List.from(results.map((x)=>x.toJson()),
};
}
班级成绩{
结果({
这位作者,
这个.说明,,
这个,这个形象,,
这个.createdAt,
});
最终作者;
最终字符串描述;
最终字符串图像;
最终日期时间创建日期;
工厂结果.fromJson(映射json)=>Result(
作者:author.fromJson(json[“author”]),
description:json[“description”],
image:json[“image”],
createdAt:DateTime.parse(json[“created_at”]),
);
映射到JSON()=>{
“author”:author.toJson(),
“描述”:描述,
“形象”:形象,
“created_at”:createdAt.toiso8601字符串(),
};
}
类作者{
作者({
这个身份证,
这个.username,
});
最终int id;
最终字符串用户名;
factory Author.fromJson(映射json)=>Author(
id:json[“id”],
用户名:json[“用户名”],
);
映射到JSON()=>{
“id”:id,
“用户名”:用户名,
};
}

谢谢您的回答。但在这种情况下,如何在提要屏幕中获取数据?我以前的代码是通过
List feeds=await DatabaseService.getPosts()实现的因为它们都是一种格式。@Davrick首先,在我提供的解决方案中,您可以将类名从Author修改为Feed。在这样做之后,如果我按照建议实现其他类,它将不再接收类型提要(List)的列表,而是类型提要(Feed)的实例,然后在小部件中可以如下调用该列表:Feed.resultsIt worked。谢谢,谢谢你的回答。但在这种情况下,如何在提要屏幕中获取数据?我以前的代码是通过
List feeds=await DatabaseService.getPosts()实现的因为它们都是一种格式。@Davrick首先,在我提供的解决方案中,您可以将类名从Author修改为Feed。在这样做之后,如果我按照建议实现其他类,它将不再接收类型提要(List)的列表,而是类型提要(Feed)的实例,然后在小部件中可以如下调用该列表:Feed.resultsIt worked。非常感谢。
{
  'count: 6,
  'next': "/posts/list/?limit=5&offset=5",
  'previous': null,'
  'results': [
      {   "author": {
                "id": 4,
                "username": "maria"
             },
            "description": "dsada",
            "image": "/media/posts/image_cropper_1616320457204.jpg",
            "created_at": "2021-03-21T09:54:38.645596Z"
        },
        {
            "author": {
                "id": 4,
                "username": "maria"
            },
            "description": "asdsad",
            "image": "/media/posts/image_cropper_1616070403137.jpg",
            "created_at": "2021-03-18T12:26:56.774797Z"
        }
   ]
   
}
class User {
    User({
        this.count,
        this.next,
        this.previous,
        this.results,
    });

    final int count;
    final String next;
    final dynamic previous;
    final List<Result> results;

    factory User.fromJson(Map<String, dynamic> json) => User(
        count: json["count"],
        next: json["next"],
        previous: json["previous"],
        results: List<Result>.from(json["results"].map((x) => Result.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "count": count,
        "next": next,
        "previous": previous,
        "results": List<dynamic>.from(results.map((x) => x.toJson())),
    };
}


class Result {
    Result({
        this.author,
        this.description,
        this.image,
        this.createdAt,
    });

    final Author author;
    final String description;
    final String image;
    final DateTime createdAt;

    factory Result.fromJson(Map<String, dynamic> json) => Result(
        author: Author.fromJson(json["author"]),
        description: json["description"],
        image: json["image"],
        createdAt: DateTime.parse(json["created_at"]),
    );

    Map<String, dynamic> toJson() => {
        "author": author.toJson(),
        "description": description,
        "image": image,
        "created_at": createdAt.toIso8601String(),
    };
}

class Author {
    Author({
        this.id,
        this.username,
    });

    final int id;
    final String username;

    factory Author.fromJson(Map<String, dynamic> json) => Author(
        id: json["id"],
        username: json["username"],
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "username": username,
    };
}