【问题标题】:Flutter : JSON Keyless array to ListFlutter:JSON无键数组到列表
【发布时间】:2021-01-14 14:46:43
【问题描述】:

我需要将一个 array(JSON 格式)的 PHP 响应加载到 Flutter List。如何在 Flutter 中编写代码?我应该使用 dart convert 吗?

这是我的 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 flutter flutter-web


【解决方案1】:

超级简单。只需使用基本的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);

【讨论】:

    猜你喜欢
    • 2020-10-20
    • 2020-05-14
    • 2012-04-26
    • 2017-10-31
    • 1970-01-01
    • 2019-05-05
    • 2013-05-03
    • 1970-01-01
    • 2014-09-12
    相关资源
    最近更新 更多