Home > Software engineering >  prasing nested json value in flutter
prasing nested json value in flutter

Time:11-10

I have a nested json value in flutter. I wanna get the value from nested json. But I can't do it.

I already tried to make a Class of nested json object. But it seems cannot cast to that object. I used jsonDecode() which is not work. When I call the Data.fromJson, ParentData is null

factory Data.fromJson(dynamic json) {
    return ReferenceData._(
      id: json["id"],
      code: json["code"],
      parentData: (json["ParentData"] != null)
          ? ParentData.fromJson(json["ParentData"])
          : json["parentRefDataValue"],
      translations: json["translations"],
      createdAt: json["createdAt"],
      updatedAt: json["updatedAt"],

    );
  }



class ParentData {
  int? id;
  String? code;
  int? sortOrder;
  List<dynamic>? translations;
  dynamic? ParentData;
  String? createdBy;
  String? updatedBy;

  parentData._({
    this.id,
    this.code,
    this.sortOrder,
    this.translations,
    this.ParentData,
    this.createdBy,
    this.updatedBy,
  });

  factory ParentData.fromJson(dynamic json) {
    return ParentData._(
        id: json['id'],
        code: json['code'],
        sortOrder: json['sortOrder'],
        translations: json["translations"],
        ParentData: json['ParentData'],
        createdBy: json['createdBy'],
        updatedBy: json['updatedBy'],   
  }
}

CodePudding user response:

{
    "statusCode": "SC0000",
    "statusMessage": "SUCCESS",
    "data": [
        {
            "id": 45,
            "code": "A3",
            "sortOrder": 2,
            "createdAt": "2022-11-09 02:33:58",
            "updatedAt": "2022-11-09 02:33:05",
            "deletedAt": null,
            "parentData": {
                "id": 44,
                "code": "Q3",
                "sortOrder": 2,
                "translations": [
                    {
                        "id": 32,
                        "locale": "EN",
                        "value": "Question 3"
                    },
                    {
                        "id": 31,
                        "locale": "KR",
                        "value": "test"
                    }
                ],
                "parentData": {
                    "id": 21,
                    "code": "2",
                    "sortOrder": 2,
                    "translations": [
                        {
                            "id": 21,
                            "locale": "EN",
                            "value": "Testing question 1"
                        },
                        {
                            "id": 22,
                            "locale": "ZH-HK",
                            "value": "Testing question 1"
                        }
                    ],
                    "parentData": null,
                    "createdBy": "UafDLP18                            ",
                    "updatedBy": "Admin",
                    "isActive": true
                },
                "createdBy": " User                            ",
                "updatedBy": " User                            ",
                "isActive": true
            },
            "referFaq": null,
            "translations": [
                {
                    "id": 33,
                    "locale": "EN",
                    "value": "Answer 3"
                },
                {
                    "id": 34,
                    "locale": "KR",
                    "value": "testingans 3"
                }
            ],
            "createdBy": "Admin",
            "updatedBy": "Admin",
            "status": "ACTIVE",
            "isActive": true
        },
        {
            "id": 43,
            "code": "A2",
            "sortOrder": 1,
            "createdAt": "2022-11-09 02:19:42",
            "updatedAt": "2022-11-09 09:24:52",
            "deletedAt": null,
            "parentData": {
                "id": 22,
                "code": "Q2",
                "sortOrder": 1,
                "translations": [
                    {
                        "id": 24,
                        "locale": "EN",
                        "value": "Question2"
                    },
                    {
                        "id": 23,
                        "locale": "KR",
                        "value": "testing3"
                    }
                ],
                "parentData": {
                    "id": 21,
                    "code": "2",
                    "sortOrder": 2,
                    "translations": [
                        {
                            "id": 22,
                            "locale": "KR,
                            "value": "Testing 1"
                        },
                        {
                            "id": 21,
                            "locale": "EN",
                            "value": "Testing"
                        }
                    ],
                    "parentData": null,
                    "createdBy": "UafDLP18                            ",
                    "updatedBy": "Admin",
                    "isActive": true
                },
                "createdBy": "Admin",
                "updatedBy": "Admin",
                "isActive": true
            },
            "referFaq": null,
            "translations": [
                {
                    "id": 30,
                    "locale": "EN",
                    "value": "Answer 2"
                },
                {
                    "id": 29,
                    "locale": "KR",
                    "value": "testing2"
                }
            ],
            "createdBy": "Admin",
            "updatedBy": "Admin",
            "status": "ACTIVE",
            "isActive": true
        }
    ]
}

CodePudding user response:

Make sure that you are providing to the fromJson method the Map<String, String> by converting json string with jsonDecode

final jsonString = api.getJsonDaya();
final json = jsonDecode(jsonString)
final data = Data.fromJson(json);

CodePudding user response:

I tried to access the id inside ParentData

questions = (await provider.getList(RefCode.QUESTIONS))
          .where((question) =>
              question.parentData?.parentData['id'] ==
              widget.categoryCode)
          .toList();

CodePudding user response:

Assuming you wanted to nest class ParentData you need to change class field dynamic? ParentData to ParentData? parentData

class ParentData {
  ParentData({
    this.id,
    this.code,
    this.sortOrder,
    this.translations,
    this.parentData,
    this.createdAt,
    this.updatedBy,
  });
  

  factory ParentData.fromJson(Map<String, dynamic> json) => ParentData(
      id: json['id'] as int?,
      code: json['code'] as String?,
      sortOrder: json['sortOrder'] as int?,
      translations: (json['translations'] as List<dynamic>?)
          ?.map((e) => e as Map<String, dynamic>)
          .toList(),
      parent: json['parent'] == null
          ? null
          : ParentData.fromJson(json['parentData'] as Map<String, dynamic>),
      createdAt: json['createdAt'] as String?,
      updatedBy: json['updatedBy'] as String?,
    );

  int? id;
  String? code;
  int? sortOrder;
  List<Map<String, dynamic>>? translations;
  ParentData? parent;
  String? createdAt;
  String? updatedBy;
}
  • Related