web-dev-qa-db-ja.com

FlutterのAndroid)と同様に、JSONをPOJO(オブジェクト)に変換します

Json応答(REST APIから)をPOJO(Androidで使用)に変換して、受信したデータをアプリケーションで使用できるようにする方法を見つけようとしています。私が受け取っているデータは少し複雑で、3つのレベルの配列(配列内の配列内の配列)が含まれているため、マップは十分ではありません。

API呼び出しにDioライブラリを使用していますが、API呼び出しを正常に実行し、問題なくコンソールにデータを出力できます。誰かがこれを達成するのを手伝ってくれる?

以下は私が得ている応答です。

{
"status": 200,
"message": "success",
"data": [
    {
        "menu": {
            "megamenu_id": "55",
            "language_id": "1",
            "title": "Mobiles",
            "description": "",
            "submenu": [
                {
                    "0": {
                        "info": {
                            "megamenu_id": "67",
                            "language_id": "1",
                            "title": "Mobile Phones",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "68",
                                "language_id": "1",
                                "title": "Android Phones",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "69",
                                "language_id": "1",
                                "title": "Smart Phones",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "70",
                                "language_id": "1",
                                "title": "Windows Phones",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "72",
                                "language_id": "1",
                                "title": "Basic Phones",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "73",
                                "language_id": "1",
                                "title": "Latest Mobiles",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "74",
                                "language_id": "1",
                                "title": "Upcoming Mobiles",
                                "description": ""
                            }
                        }
                    ]
                },
                {
                    "0": {
                        "info": {
                            "megamenu_id": "75",
                            "language_id": "1",
                            "title": "Brands",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "76",
                                "language_id": "1",
                                "title": "Samsung",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "77",
                                "language_id": "1",
                                "title": "Apple",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "78",
                                "language_id": "1",
                                "title": "Redmi",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "79",
                                "language_id": "1",
                                "title": "Nokia",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "81",
                                "language_id": "1",
                                "title": "Sony",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "82",
                                "language_id": "1",
                                "title": "Micromax",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "83",
                                "language_id": "1",
                                "title": "HTC",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "84",
                                "language_id": "1",
                                "title": "Motorola",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "85",
                                "language_id": "1",
                                "title": "Oppo",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "86",
                                "language_id": "1",
                                "title": "Vivo",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "87",
                                "language_id": "1",
                                "title": "Honor",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "88",
                                "language_id": "1",
                                "title": "Oneplus",
                                "description": ""
                            }
                        }
                    ]
                },
                {
                    "0": {
                        "info": {
                            "megamenu_id": "94",
                            "language_id": "1",
                            "title": "Mobile Accessories",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "95",
                                "language_id": "1",
                                "title": "Bluetooth Headsets",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "96",
                                "language_id": "1",
                                "title": "Mobile Chargers",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "97",
                                "language_id": "1",
                                "title": "Mobiles Cases & Covers",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "98",
                                "language_id": "1",
                                "title": "Screen Protector",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "99",
                                "language_id": "1",
                                "title": "Mobile Batteries",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "100",
                                "language_id": "1",
                                "title": "Headphones & Headsets",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "101",
                                "language_id": "1",
                                "title": "Memory Cards",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "102",
                                "language_id": "1",
                                "title": "Power Banks",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "103",
                                "language_id": "1",
                                "title": "Stylus",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "104",
                                "language_id": "1",
                                "title": "Mobile Docks",
                                "description": ""
                            }
                        }
                    ]
                },
                {
                    "0": {
                        "info": {
                            "megamenu_id": "107",
                            "language_id": "1",
                            "title": "Features",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "108",
                                "language_id": "1",
                                "title": "Dual Sim Phones",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "111",
                                "language_id": "1",
                                "title": "4GB Ram Mobiles",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "112",
                                "language_id": "1",
                                "title": "Rear Camera [13 MP]",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "113",
                                "language_id": "1",
                                "title": "Screen Size [4.5 - 5.2 Inch]",
                                "description": ""
                            }
                        }
                    ]
                }
            ]
        }
    },

    {
        "menu": {
            "megamenu_id": "168",
            "language_id": "1",
            "title": "Fashion",
            "description": "",
            "submenu": [
                {
                    "0": {
                        "info": {
                            "megamenu_id": "169",
                            "language_id": "1",
                            "title": "Clothing",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "170",
                                "language_id": "1",
                                "title": "T-Shirts",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "171",
                                "language_id": "1",
                                "title": "Shirts",
                                "description": ""
                            }
                        }
                    ]
                },
                {
                    "0": {
                        "info": {
                            "megamenu_id": "178",
                            "language_id": "1",
                            "title": "Womens Clothing",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "187",
                                "language_id": "1",
                                "title": "Night Wear",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "188",
                                "language_id": "1",
                                "title": "Tunics",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "189",
                                "language_id": "1",
                                "title": "Kurtis",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "190",
                                "language_id": "1",
                                "title": "Sarees",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "191",
                                "language_id": "1",
                                "title": "Lehengas",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "192",
                                "language_id": "1",
                                "title": "Swimming Suit",
                                "description": ""
                            }
                        }
                    ]
                },
                {
                    "0": {
                        "info": {
                            "megamenu_id": "193",
                            "language_id": "1",
                            "title": "Men's Footwear",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "194",
                                "language_id": "1",
                                "title": "Shoes",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "195",
                                "language_id": "1",
                                "title": "Sneakers",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "196",
                                "language_id": "1",
                                "title": "Sandals & Floaters",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "197",
                                "language_id": "1",
                                "title": "Slippers & Flip Flops",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "198",
                                "language_id": "1",
                                "title": "Loafers",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "199",
                                "language_id": "1",
                                "title": "Boots",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "200",
                                "language_id": "1",
                                "title": "Shoe Care",
                                "description": ""
                            }
                        }
                    ]
                },
                {
                    "0": {
                        "info": {
                            "megamenu_id": "201",
                            "language_id": "1",
                            "title": "Women's Jewellery",
                            "description": ""
                        }
                    },
                    "subsubmenu": [
                        {
                            "infosub": {
                                "megamenu_id": "202",
                                "language_id": "1",
                                "title": "Earrings",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "203",
                                "language_id": "1",
                                "title": "Bangles",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "204",
                                "language_id": "1",
                                "title": "Bracelets",
                                "description": ""
                            }
                        },
                        {
                            "infosub": {
                                "megamenu_id": "205",
                                "language_id": "1",
                                "title": "Rings",
                                "description": ""
                            }
                        }
                    ]
                }
            ]
        }
    },
    {
        "menu": {
            "megamenu_id": "220",
            "language_id": "1",
            "title": "Care",
            "description": ""
        }
    },
    {
        "menu": {
            "megamenu_id": "221",
            "language_id": "1",
            "title": "Offers",
            "description": ""
        }
    },
    {
        "menu": {
            "megamenu_id": "222",
            "language_id": "1",
            "title": "News",
            "description": ""
        }
    }
]
}
6
Jaswant Singh

そのため、json_serializableライブラリを試してみた後、独自のソリューションを思いつきました。外部ライブラリはまったく必要なく、魅力のように機能します。このようにして、ボイラープレートコードを少なくする必要があり、これを行うためのクリーンな方法だと思います。

モデルの作り方は次のとおりです

class SideMenuRes {
final int status;
final String message;
final List<SideMenuDatum> sideMenuData;

SideMenuRes({this.status, this.message, this.sideMenuData});
factory SideMenuRes.fromJson(Map json) {
  return SideMenuRes(
    status: json['status'],
    message: json['message'],
    sideMenuData: json['status'] == 200 ? (json['data'] as List).map((i) => new 
  SideMenuDatum.fromJson(i)).toList() : null,
  );
}
}

class SideMenuDatum {
final Menu menu;
SideMenuDatum({this.menu});
factory SideMenuDatum.fromJson(Map json) {
  return SideMenuDatum(
    menu: Menu.fromJson(json['menu']),
  );
}
}

class Menu {
final String megamenu_id;
final String language_id;
final String title;
final String description;
final List<SubMenu> subMenu;

Menu({this.megamenu_id, this.language_id, this.title, this.description, this.subMenu});
factory Menu.fromJson(Map json) {
  return Menu(
      megamenu_id: json['megamenu_id'],
      language_id: json['language_id'],
      title: json['title'],
      description: json['description'],
      subMenu: json['submenu'] != null ? (json['submenu'] as List).map((i) => new SubMenu.fromJson(i)).toList() : null
  );
}
}

class SubMenu {
final Zero zero;
final List<SubSubMenu> subSubMenu;

SubMenu({this.zero, this.subSubMenu});
factory SubMenu.fromJson(Map json) {
  return SubMenu(
      zero: Zero.fromJson(json['0']),
      subSubMenu: (json['subsubmenu'] as List).map((i) => new SubSubMenu.fromJson(i)).toList()
  );
}
}

class Zero {
final Info info;
Zero({this.info});
factory Zero.fromJson(Map json) {
  return Zero(
    info: Info.fromJson(json['info']),
  );
  }
 }

class SubSubMenu {
final InfoSub infoSub;
SubSubMenu({this.infoSub});

factory SubSubMenu.fromJson(Map json) {
  return SubSubMenu(
      infoSub: InfoSub.fromJson(json['infosub'])
  );
}
}

class InfoSub {
final String megamenu_id;
final String language_id;
final String title;
final String description;

InfoSub({this.megamenu_id, this.language_id, this.title, this.description});
factory InfoSub.fromJson(Map json) {
return InfoSub(
    megamenu_id: json['megamenu_id'],
    language_id: json['language_id'],
    title: json['title'],
    description: json['description']
  );
 }
}

class Info {
 final String megamenu_id;
 final String language_id;
final String title;
final String description;

Info({this.megamenu_id, this.language_id, this.title, this.description});
factory Info.fromJson(Map json) {
return Info(
    megamenu_id: json['megamenu_id'],
    language_id: json['language_id'],
    title: json['title'],
    description: json['description']
  );
 }
}

そしてそれをこのように呼ぶ

SubMenuRes subMenuRes = SubMenuRes.fromJson(response.data);

それでおしまい!

1
Jaswant Singh

Android Studioプラグイン: https://plugins.jetbrains.com/plugin/11460-json2Dart/

オンライン: https://javiercbk.github.io/json_to_Dart/

手動で解析: https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51

JsonからDartクラスを生成する

JSONをDartクラスに変換する

JSONまたはJSON-SchemaからDartクラスを生成します。

enter image description here

5
Amit Prajapati

json_serializable は十分に文書化されていませんが、特に配列に関しては、built_valueよりも使いやすく、必要な定型文が少なくて済みます。

import 'package:json_annotation/json_annotation.Dart';
import 'Dart:convert';

part 'school.g.Dart';

@JsonSerializable()
class School {
  final String name;

  final int maxStudentCount;

  final List<Student> students;

  School(this.name, this.maxStudentCount, this.students);
  factory School.fromJson(Map<String, dynamic> json) => _$SchoolFromJson(json);
  Map<String, dynamic> toJson() => _$SchoolToJson(this);
}

@JsonSerializable()
class Student {
  final String name;

  final DateTime birthDate;

  Student({this.name, this.birthDate});
  factory Student.fromJson(Map<String, dynamic> json) => _$StudentFromJson(json);
  Map<String, dynamic> toJson() => _$StudentToJson(this);
}

test() {
  String jsonString = '''{
   "name":"Trump University",
   "maxStudentCount":9999,
   "students":[
      {
         "name":"Peter Parker",
         "birthDate":"1999-01-01T00:00:00.000Z"
      }
   ]
  }''';

  final decodedJson = json.decode(jsonString);

  final school = School.fromJson(decodedJson);

  assert(school.students.length == 1);
}

また、列挙型のシリアル化もサポートしています。

シリアル化コードを生成するには、次のコマンドを実行します。

flutter packages pub run build_runner build
3
boformer

これは、built_valueを使用して実行できます。詳細なドキュメントは、この リンク にあります。

ボイラープレートコードを記述して、このコマンドを実行するだけですflutter packages pub run build_runner build

以下は、POJOのようなサンプルクラスです。

import 'package:built_value/built_value.Dart';
import 'package:built_value/serializer.Dart';

part 'auth.g.Dart';

abstract class Auth implements Built<Auth, AuthBuilder> {
  static Serializer<Auth> get serializer => _$authSerializer;

  String get currentServerTime;
  int get defaultOrganization;
  String get tokenExpiryTimeInMs;
  bool get rememberMe;
  int get failedLoginAttempts;
  int get userId;
  String get status;
  String get token;

  Auth._();
  factory Auth([updates(AuthBuilder b)]) = _$Auth;
}

以下はSerializerクラスです。

library serializers;

import 'package:built_value/serializer.Dart';
import 'package:built_value/standard_json_plugin.Dart';
import 'auth/auth.Dart';

part 'serializers.g.Dart';

@SerializersFor(const [
  Auth,
])

Serializers serializers = _$serializers;

Serializers standardSerializers =
(serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();

以下は、JSONからオブジェクトへの変換が行われるコードです。

Auth auth = standardSerializers.deserializeWith(
        Auth.serializer, json.decode(res.body)['user']);

お役に立てれば。

1
Thanthu

この投稿 を見て、目標に最適なオプションを取得してください

0
Morteza Rastgoo