1

I am using WordPress Api's for my flutter application. I have model class(POJO) in which there is ArrayList inside another arrayList.I don't know how to convert it to model class. Please Help. The Json is:

        wp:term: [
[
{
id: 22,
link: "https://example.com/category/ut/xyz/",
name: "XYZ",
slug: "xyx",
taxonomy: "category",
}
],
]

Sunil
  • 143
  • 1
  • 2
  • 11
  • 1
    For me i used app.quicktype .io to generate class model for me by pasting it – Arbiter Chil Oct 09 '21 at 12:45
  • If you don't understand serialization and just want a quick way to test your api, follow @ArbiterChil. If not, I recommend checking the Flutter dev guide https://flutter.dev/docs/development/data-and-backend/json – IcyHerrscher Oct 09 '21 at 15:41

1 Answers1

1

Maybe you meant it:

 class Obj {
       Obj({
        this.wpTerm,
     });

    final List<List<WpTerm>> wpTerm;

    factory Obj.fromJson(Map<String, dynamic> json) => Obj(
        wpTerm: List<List<WpTerm>>.from(json["wp:term"].map((x) => List<WpTerm>.from(x.map((x) => WpTerm.fromJson(x))))),
    );

    Map<String, dynamic> toJson() => {
        "wp:term": List<dynamic>.from(wpTerm.map((x) => List<dynamic>.from(x.map((x) => x.toJson())))),
    };
    }

    class WpTerm {
    WpTerm({
        this.id,
        this.link,
        this.name,
        this.slug,
        this.taxonomy,
    });

    final int id;
    final String link;
    final String name;
    final String slug;
    final String taxonomy;

    factory WpTerm.fromJson(Map<String, dynamic> json) => WpTerm(
        id: json["id"],
        link: json["link"],
        name: json["name"],
        slug: json["slug"],
        taxonomy: json["taxonomy"],
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "link": link,
        "name": name,
        "slug": slug,
        "taxonomy": taxonomy,
    };
    }

//https://app.quicktype.io/

BadBoy
  • 11
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Muhammedogz Oct 11 '21 at 20:14