تحويل JSON إلى Dart class لتطبيقات Flutter

تكتب الأداة لكل كائن صنفا بحقول final ومنشئ بمعاملات مسماة ودالتي تحويل من JSON وإليه.

جارٍ تحميل الأداة…

المعالجة والخصوصية: تُحسب النتيجة داخل متصفحك ولا تُرسل بياناتك إلى أي خادم.

طريقة الاستخدام

  1. أدخل نص JSON واسم النوع الرئيسي.
  2. تظهر النتيجة فورًا وتتحدّث مع كل تعديل.
  3. انسخ النتيجة، أو انسخ رابطًا يحفظ القيم نفسها لمشاركتها.

طريقة الحساب

int وdouble وString وbool وList<T> وdynamic، وصنف لكل كائن. القيمة null أو الغياب تجعل النوع nullable مثل int?. الأرقام تقرأ عبر num ثم toInt أو toDouble لأن JSON قد يرسل 10 بدل 10.0، والكائنات المتداخلة تقرأ عبر fromJson الخاص بها.

مثال

المدخلات: اسم النوع الرئيسي = Root. النتيجة: الكود الناتج: class Root { final int id; final String name; final String email; final bool isActive; final double balance; final List<String> tags; final Address address; final List<Order> orders; Root({ required this.id, required this.name, required this.email, required this.isActive, required this.balance, required this.tags, required this.address, required this.orders, }); factory Root.fromJson(Map<String, dynamic> json) => Root( id: (json['id'] as num).toInt(), name: json['name'] as String, email: json['email'] as String, isActive: json['is_active'] as bool, balance: (json['balance'] as num).toDouble(), tags: (json['tags'] as List<dynamic>).map((e) => e as String).toList(), address: Address.fromJson(json['address'] as Map<String, dynamic>), orders: (json['orders'] as List<dynamic>).map((e) => Order.fromJson(e as Map<String, dynamic>)).toList(), ); Map<String, dynamic> toJson() => { 'id': id, 'name': name, 'email': email, 'is_active': isActive, 'balance': balance, 'tags': tags, 'address': address.toJson(), 'orders': orders.map((e) => e.toJson()).toList(), }; } class Address { final String city; final String postalCode; Address({ required this.city, required this.postalCode, }); factory Address.fromJson(Map<String, dynamic> json) => Address( city: json['city'] as String, postalCode: json['postal_code'] as String, ); Map<String, dynamic> toJson() => { 'city': city, 'postal_code': postalCode, }; } class Order { final String orderId; final double total; final String? coupon; Order({ required this.orderId, required this.total, this.coupon, }); factory Order.fromJson(Map<String, dynamic> json) => Order( orderId: json['order_id'] as String, total: (json['total'] as num).toDouble(), coupon: json['coupon'] as String?, ); Map<String, dynamic> toJson() => { 'order_id': orderId, 'total': total, 'coupon': coupon, }; }، عدد الأنواع: 3، عدد الحقول: 13.

{
  "id": 1024,
  "name": "Sara Ahmed",
  "email": "sara@example.com",
  "is_active": true,
  "balance": 1520.75,
  "tags": ["vip", "riyadh"],
  "address": { "city": "Riyadh", "postal_code": "12211" },
  "orders": [
    { "order_id": "A-1", "total": 99.5, "coupon": "EID10" },
    { "order_id": "A-2", "total": 120, "coupon": null }
  ]
}
class Root {
  final int id;
  final String name;
  final String email;
  final bool isActive;
  final double balance;
  final List<String> tags;
  final Address address;
  final List<Order> orders;

  Root({
    required this.id,
    required this.name,
    required this.email,
    required this.isActive,
    required this.balance,
    required this.tags,
    required this.address,
    required this.orders,
  });

  factory Root.fromJson(Map<String, dynamic> json) => Root(
        id: (json['id'] as num).toInt(),
        name: json['name'] as String,
        email: json['email'] as String,
        isActive: json['is_active'] as bool,
        balance: (json['balance'] as num).toDouble(),
        tags: (json['tags'] as List<dynamic>).map((e) => e as String).toList(),
        address: Address.fromJson(json['address'] as Map<String, dynamic>),
        orders: (json['orders'] as List<dynamic>).map((e) => Order.fromJson(e as Map<String, dynamic>)).toList(),
      );

  Map<String, dynamic> toJson() => {
        'id': id,
        'name': name,
        'email': email,
        'is_active': isActive,
        'balance': balance,
        'tags': tags,
        'address': address.toJson(),
        'orders': orders.map((e) => e.toJson()).toList(),
      };
}

class Address {
  final String city;
  final String postalCode;

  Address({
    required this.city,
    required this.postalCode,
  });

  factory Address.fromJson(Map<String, dynamic> json) => Address(
        city: json['city'] as String,
        postalCode: json['postal_code'] as String,
      );

  Map<String, dynamic> toJson() => {
        'city': city,
        'postal_code': postalCode,
      };
}

class Order {
  final String orderId;
  final double total;
  final String? coupon;

  Order({
    required this.orderId,
    required this.total,
    this.coupon,
  });

  factory Order.fromJson(Map<String, dynamic> json) => Order(
        orderId: json['order_id'] as String,
        total: (json['total'] as num).toDouble(),
        coupon: json['coupon'] as String?,
      );

  Map<String, dynamic> toJson() => {
        'order_id': orderId,
        'total': total,
        'coupon': coupon,
      };
}

الحدود والملاحظات

  • لا يستخدم json_serializable أو freezed، فالكود مستقل ولا يحتاج build_runner.
  • المفتاح غير الصالح كاسم يتحول إلى camelCase، ويبقى المفتاح الأصلي داخل fromJson وtoJson.

أسئلة شائعة عن هذه الأداة

لماذا تقرأ الأرقام عبر num؟

jsonDecode يعيد 10 كنوع int و10.5 كنوع double، والقراءة عبر num تمنع خطأ التحويل عند اختلاف الشكل.

كيف أستخدم الصنف مع استجابة API؟

استدع Root.fromJson(jsonDecode(response.body)) للقراءة، وjsonEncode(root.toJson()) للإرسال.

هل ترسل بياناتي إلى خادم؟

لا، التحويل يعمل داخل متصفحك ولا يغادر نص JSON جهازك.