Skip to content

Instantly share code, notes, and snippets.

@stegrams
Last active December 30, 2020 12:37
Show Gist options
  • Save stegrams/ecb21e51e8274919e38d3d2eb84ac777 to your computer and use it in GitHub Desktop.
Save stegrams/ecb21e51e8274919e38d3d2eb84ac777 to your computer and use it in GitHub Desktop.
Decoding a JSON response in a concise way (version 1) and in a more "official way" (version 2)
// Version 2: The official way based on https://jsontodart.com converter, with a grain of my salt
import 'dart:convert';
void main() {
List<SmoothieModel> smoothies = jsonDecode(json_str)
.map<SmoothieModel>((s) => SmoothieModel.fromJson(s))
.toList();
print(smoothies);
}
class SmoothieModel {
String smoothieName;
List<String> ingredients = [];
SmoothieModel({this.smoothieName, this.ingredients});
SmoothieModel.fromJson(Map<String, dynamic> json) {
smoothieName = json['smoothie_name'];
if (json['ingredients'] != null) {
ingredients = json['ingredients']
.map<String>((i) => Ingredient.fromJson(i).localizedFoodItemName.en)
.toList();
}
}
String toString() =>
"{smoothieName: $smoothieName, ingredients: $ingredients}";
}
class Ingredient {
LocalizedFoodItemName localizedFoodItemName;
Ingredient({
this.localizedFoodItemName,
});
Ingredient.fromJson(Map<String, dynamic> json) {
localizedFoodItemName = json['localizedFoodItemNames'] != null
? LocalizedFoodItemName.fromJson(json['localizedFoodItemNames'])
: LocalizedFoodItemName();
}
String toString() => "{localizedFoodItemName: $localizedFoodItemName}";
}
class LocalizedFoodItemName {
String en;
String de;
String pt;
LocalizedFoodItemName({this.en, this.de, this.pt});
LocalizedFoodItemName.fromJson(Map<String, dynamic> json) {
en = json['en'];
de = json['de'];
pt = json['pt'];
}
String toString() => "{en: $en, de: $de, pt: $pt}";
}
var json_str = """
[{
"smoothie_name": "Crazy Colada",
"calories": 320,
"description": "Enjoy the tropical flavours of coconut and pineapple!",
"image_path": "assets/smoothie/crazy-colada.jpg",
"isFavourite": false,
"ingredients": [
{
"identifier": "pineapple",
"localizedFoodItemNames": {
"en": "Pineapple",
"de": "Ananas",
"pt": "Abacaxi"
},
"referenceMass": "165 g",
"density": "165 g per 1 cup",
"totalSaturatedFat": "0 g",
"totalMonounsaturatedFat": "0 g",
"totalPolyunsaturatedFat": "0.1 g",
"cholesterol": "0 g",
"sodium": "2 mg",
"totalCarbohydrates": "22 g",
"dietaryFiber": "2.3 g",
"sugar": "16 g",
"protein": "0.9 g",
"calcium": "21.4 mg",
"potassium": "285 mg",
"vitaminA": "0.051 mg",
"vitaminC": "27.9 mg",
"iron": "0.412 mg"
},
{
"identifier": "almond-milk",
"localizedFoodItemNames": {
"en": "Almond Milk",
"de": "Mandelmilch",
"pt": "Leite de Amêndoas"
},
"referenceMass": "240 g",
"density": "240 g per 1 cup",
"totalSaturatedFat": "0 g",
"totalMonounsaturatedFat": "3 g",
"totalPolyunsaturatedFat": "1 g",
"cholesterol": "0 g",
"sodium": "220 mg",
"totalCarbohydrates": "1 g",
"dietaryFiber": "2 g",
"sugar": "0 g",
"protein": "10 g",
"calcium": "482 mg",
"potassium": "160 mg",
"vitaminA": "0 g",
"vitaminC": "0 mg",
"iron": "1 mg"
},
{
"identifier": "coconut",
"localizedFoodItemNames": {
"en": "Coconut",
"de": "Kokosnuss",
"pt": "Coco"
},
"referenceMass": "80 g",
"density": "80 g per 1 cup",
"totalSaturatedFat": "23.8 g",
"totalMonounsaturatedFat": "1.14 g",
"totalPolyunsaturatedFat": "0.293 g",
"cholesterol": "0 g",
"sodium": "16 mg",
"totalCarbohydrates": "12.2 g",
"dietaryFiber": "7.2 g",
"sugar": "4.98 g",
"protein": "2.66 g",
"calcium": "11.2 mg",
"potassium": "285 mg",
"vitaminA": "0 mg",
"vitaminC": "2.64 mg",
"iron": "1.94 mg"
}
]
}]
""";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment