【问题标题】:Flutter: Unexpected character (at character 1)Flutter:意外字符(在字符 1 处)
【发布时间】:2020-06-16 22:42:40
【问题描述】:

在 Flutter 中,无论我尝试解码什么 json 文件,我都会得到与上述标题相同的错误。 最初我认为这与我的项目有关,但在开始一个新的模板 Flutter 项目后,我仍然收到相同的错误。我的根文件夹中有 json 文件,并将它们添加到 pubspec.yaml 文件中:

  assets:
      - Sample-JSON-data.json
      - Sample-employee-JSON-data.json

目前的Main.dart代码:

    var jsonString = 'Sample-JSON-data.json';
    var response = jsonDecode(jsonString);
    print(response);

我已经在多个测试站点上验证了我的 json 数据,并在Flutter Documentation 中尝试了各种方法。 json数据:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.0763783444317,
                    -33.98045132346684
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.07774912725728,
                    -33.97470462237792
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.07774912725728,
                    -33.97470462237792
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.07774912725728,
                    -33.97470462237792
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.0763783444317,
                    -33.98045132346684
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.07774912725728,
                    -33.97470462237792
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.07774912725728,
                    -33.97470462237792
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.07774912725728,
                    -33.97470462237792
                ]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    151.0763783444317,
                    -33.98045132346684
                ]
            }
        }
    ]
}

【问题讨论】:

    标签: json parsing flutter dart


    【解决方案1】:

    jsonDecode 期望输入是有效的 JSON 序列化为字符串 fe。 { "message": "Hello World!" }。所以这会起作用:jsonDecode('{"message": "Hello World!"}')

    您传递给它的是一个文件名,它不会自动为您读取文件。您可以在此处查看操作方法:

    Flutter - Read text file from assets

    【讨论】:

    • 谢谢,这非常有帮助。我误解了序列化。
    【解决方案2】:

    实际上你并没有从 json 文件中加载数据。

    试试:

    var jsonString = await rootBundle.loadString('Sample-JSON-data.json')
    

    【讨论】:

    • 谢谢,我以前用过这个,我一定有一个大脑模糊的情况,所以感谢上帝你提出来了。
    猜你喜欢
    • 2019-09-04
    • 2021-08-29
    • 1970-01-01
    • 2021-03-30
    • 2021-10-10
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多