Flutter : JSON 无键数组列表
Flutter : JSON Keyless array to List
我需要将 PHP 响应加载到 Flutter List。如何在 Flutter 中编写代码?我应该使用飞镖转换吗?
这是我的 PHP 回复:
[
"Butter",
"Cheese / Keju",
"Cream",
"Fresh Juice",
"Frozen Food Beef",
"Frozen Food Chicken",
"Frozen Food Pork",
"Frozen Food Potatoes",
"Frozen Food Vegetables",
"Jam & Spread",
"Mayonais",
"Salad Dressing",
"Sereal & Muesli",
"Snack & Biscuit",
"Sour Cream",
"Soya Milk",
"Whipping Cream",
"Yogurt",
"Yogurt Drink"
]
超级简单。只需使用基础 json
库。
import 'dart:convert';
// assume you got this string from a php request
String fromPhp = '[ "Butter", "Cheese / Keju", "Cream", "Fresh Juice", "Frozen Food Beef", "Frozen Food Chicken", "Frozen Food Pork", "Frozen Food Potatoes", "Frozen Food Vegetables", "Jam & Spread", "Mayonais", "Salad Dressing", "Sereal & Muesli", "Snack & Biscuit", "Sour Cream", "Soya Milk", "Whipping Cream", "Yogurt", "Yogurt Drink" ]';
// use this to convert it to a dart list of string
// note that the only tricky part is that json.decode will return
// a List<dynamic> so I am using List.from to convert it to List<String>
List<String> dartList = List.from(json.decode(fromPhp));
print(dartList);
我需要将 PHP 响应加载到 Flutter List。如何在 Flutter 中编写代码?我应该使用飞镖转换吗?
这是我的 PHP 回复:
[
"Butter",
"Cheese / Keju",
"Cream",
"Fresh Juice",
"Frozen Food Beef",
"Frozen Food Chicken",
"Frozen Food Pork",
"Frozen Food Potatoes",
"Frozen Food Vegetables",
"Jam & Spread",
"Mayonais",
"Salad Dressing",
"Sereal & Muesli",
"Snack & Biscuit",
"Sour Cream",
"Soya Milk",
"Whipping Cream",
"Yogurt",
"Yogurt Drink"
]
超级简单。只需使用基础 json
库。
import 'dart:convert';
// assume you got this string from a php request
String fromPhp = '[ "Butter", "Cheese / Keju", "Cream", "Fresh Juice", "Frozen Food Beef", "Frozen Food Chicken", "Frozen Food Pork", "Frozen Food Potatoes", "Frozen Food Vegetables", "Jam & Spread", "Mayonais", "Salad Dressing", "Sereal & Muesli", "Snack & Biscuit", "Sour Cream", "Soya Milk", "Whipping Cream", "Yogurt", "Yogurt Drink" ]';
// use this to convert it to a dart list of string
// note that the only tricky part is that json.decode will return
// a List<dynamic> so I am using List.from to convert it to List<String>
List<String> dartList = List.from(json.decode(fromPhp));
print(dartList);