Dart convert array of json objects to list of objects

Janaka

I have array of country json objects like this

[{"id":4,"name":"Afghanistan","alpha2":"af","alpha3":"afg"},
        {"id":8,"name":"Albania","alpha2":"al","alpha3":"alb"},
        {"id":12,"name":"Algeria","alpha2":"dz","alpha3":"dza"},..

I have got them to a map like this

Map<String, dynamic> jsonMap;
jsonMap = json.decode(jsonString);

After this I have the data like this

[{id: 4, name: Afghanistan, alpha2: af, alpha3: afg}, {id: 8, name: Albania, alpha2: al, alpha3: alb}, ...

What i want to do is create a List of county objects from this

Country
{
int id;
String name;
String alpha2;
String alpha3;
}

Can anyone explain how to do this conversion ?

Maz341

I use this straight forward way:

List<TestModel> myTestModel;
var response = await http.get("myTestUrl");

myTestModel=(json.decode(response.body) as List).map((i) =>
              TestModel.fromJson(i)).toList();

The link can be really helpful: json_serializable

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert objects inside the array - JSON to Dart List with serialized model?

convert list of objects to Json array

Dart: convert Map to List of Objects

Convert list of objects to map - dart

Convert JSON with nested objects to list array in JS

Convert array to list of objects

convert array of objects to array of list

Convert array of json objects into json

How to convert JSON array objects into list array | Swift 5

Convert JSON into Array of JavaScript Objects

How to convert JSON array to objects

Convert Json objects Collection to Array

Python Convert JSON Array to Corresponding List of Python Objects

Convert list of Objects to JSON Array in java - Spring Boot api testing

Convert String containing list of JSON objects to Object Array

Using Retrofit to convert from to array of JSON objects to List

Parsing JSON that has a nested array of objects in Dart?

JSON array to List of objects: UnrecognizedPropertyException

Deserialize array of Json into list of objects

Converting list of objects to json array

Convert from List of string array to List of objects

Convert list of strings to list of json objects in pyspark

Convert list of objects to numpy array without coercing objects into array

How do you convert a list of json objects to a list of javascript objects?

List of Class Objects in Dart

Sort a list of objects in Dart

Convert array of strings to array of objects in JSON property

Convert array of elements to array of JSON objects

how to convert an array into a json array objects