4
{
"transactionId": "11f8ecc05273e35a4eb2dc1a",
"type": "REQUEST_FOR_HH_INTERVIEW",
"answers": {
  "selectProvinceDistrictCommuneVillage": {
    "value": "01020706"
  },
  "positionOfOfficial": {
    "value": "Province Officer"
  },
  "enterKhmerName": {
    "value": "សុខ"
  },
  "selectSex": {
    "value": "MALE"
  },
  "dob": {
    "value": "1994-06-15T03:27:47.409Z"
  },
  "areYouMarried": {
    "value": "YES"
  },
  "scanSpousesID": {
    "value": "435465"
  },
  "enterSpousesKhmerName": {
    "value": "នារី"
  },
  "selectSexSpouse": {
    "value": "FEMALE"
  },
  "dobSpouse": {
    "value": "1996-08-15T03:27:47.409"
  },
  "numberOfMales": {
    "value": "4"
  },
  "numberOfFemales": {
    "value": "5"
  },
  "selectReasonForRequesting": {
    "value": [
      "NATURAL_DISASTER"
    ]
  }
}
}

So this is the JSON I need to parse into the dart model. The problem I am having with this structure is that map inside answers are all dynamic. Also, the number of the maps inside answers is not always the same. For example, the next JSON response can be.

{
 "transactionId": "11f8ecc05273e35a4eb2dc1a",
 "type": "REQUEST_FOR_HH_INTERVIEW",
 "answers": {
   "selectCode": {
   "value": "01020706"
  },
  "selectRoomValue": {
  "value": "1996-08-15T03:27:47.409"
 },
 "numberOfFamilyMembers": {
  "value": "4"
  },
 "selectFoods": {
   "value": [
     "Piza",
     "Burger"
   ]
  }
 }
}

which is different from the first response. I need to make a dart model that parses both responses.

1 Answer 1

3

This is relatively easy to do by using a "sub-model" of Answers which would be stored within the assumed InterviewRequest model.

For example:

class InterviewRequest {
  final Answers answers;
  final String transactionId;

  factory InterviewRequest.fromJson(Map<String, dynamic> json) {
    return InterviewRequest(
      answers: Answers.fromJson(json['answers']),
      transactionId: json['transactionId'] as String,
    );
  }
}

class Answers {
  final List<Answer> answers;

  factory Answers.fromJson(Map<String, dynamic> json) {
    List answers = [];
    for (String question in json.keys)
      answers.add(Answer(question, json[key]));
    return Answers(answers);
  }
}

Sign up to request clarification or add additional context in comments.

9 Comments

Can you kindly also mention how the sub-model will look.
@AabhashRai The sub-model is just another class, and would look very similar to the InterviewRequest class.
I wasn't able to make a submodel for the answer. I don't think there is a way to make a submodel as an interview for an answer because the fields inside the answer are all dynamic. So I just kept the answer as type dynamic. ie final dynamic answer;
No offense, but you shouldn't be asking people on the internet to "do your homework". That said, there is no such thing as a "sub-model" in Dart. Or even a "model" for that matter. It's just a term. At the end of the day, these are just classes, and we as developers have the control over their function. I showed you how to do it, and provided you with the information you need to do the rest. Please mark the answer as accepted, and good luck with your interview.
No offense taken :). I would have marked your answer accepted if it did anything for me. anyway, thank you for taking some time to help me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.