简体   繁体   中英

Parsing JSON to a model in Flutter

I have some data stored in JSON that I am trying to map to it's respective model class.

{ 
"1":{ 
  "answer":"This is question 1",
  "question":"This is answer 1"
},
"2":{ 
  "answer":"This is question 2",
  "question":"This is answer 2"
},
"3":{ 
  "answer":"This is question 3",
  "question":"This is answer 3"
},
"4":{ 
  "answer":"This is question 4",
  "question":"This is answer 4"
},
"5":{ 
  "answer":"This is question 5",
  "question":"This is answer 5"
   }
}

I am wanting to map the question and answer fields to the model however how can i achieve this when they are nested against a number? Usually I would create another model class of the parent category but what do you do when the parent category isn't labelled?

class FaqModel {
  String question;
  String answer;

  FaqModel({this.question, this.answer});

  FaqModel.fromJson(Map<String, dynamic> json) {
    question = json["question"];
    answer = json["answer"];
  }
}

Just get the keys and iterate, like this:


    final data = { 
"1":{ 
  "answer":"This is question 1",
  "question":"This is answer 1"
},
"2":{ 
  "answer":"This is question 2",
  "question":"This is answer 2"
},
"3":{ 
  "answer":"This is question 3",
  "question":"This is answer 3"
},
"4":{ 
  "answer":"This is question 4",
  "question":"This is answer 4"
},
"5":{ 
  "answer":"This is question 5",
  "question":"This is answer 5"
   }
};


  final models = data.keys.map((key) {
    final subData = data[key];
    return FaqModel.fromJson(subData);
  });

 models.forEach((item) {
   print("FAQ: ${item.question} \n ${item.answer}\n\n");
 });

Just use below model

// To parse this JSON data, do
//
//     final faqModel = faqModelFromJson(jsonString);

import 'dart:convert';

Map<String, FaqModel> faqModelFromJson(String str) =>     Map.from(json.decode(str)).map((k, v) => MapEntry<String, FaqModel>(k,     FaqModel.fromJson(v)));

String faqModelToJson(Map<String, FaqModel> data) =>     json.encode(Map.from(data).map((k, v) => MapEntry<String, dynamic>(k, v.toJson())));

class FaqModel {
    String answer;
    String question;

    FaqModel({
        this.answer,
        this.question,
    });

    factory FaqModel.fromJson(Map<String, dynamic> json) => FaqModel(
        answer: json["answer"] == null ? null : json["answer"],
        question: json["question"] == null ? null : json["question"],
    );

    Map<String, dynamic> toJson() => {
        "answer": answer == null ? null : answer,
        "question": question == null ? null : question,
    };
}

Your JSON

final jsonData = { 
"1":{ 
  "answer":"This is question 1",
  "question":"This is answer 1"
},
"2":{ 
  "answer":"This is question 2",
  "question":"This is answer 2"
},
"3":{ 
  "answer":"This is question 3",
  "question":"This is answer 3"
},
"4":{ 
  "answer":"This is question 4",
  "question":"This is answer 4"
},
"5":{ 
  "answer":"This is question 5",
  "question":"This is answer 5"
}
};

and to parse JSON

FaqModel faqModel = faqModelFromJson(jsonData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM