【问题标题】:Flutter reads JSON just partiallyFlutter 只读取部分 JSON
【发布时间】:2021-04-05 09:01:20
【问题描述】:

我正在使用 Flutter / Dart 创建一个小应用程序,它运行良好。我的资产在pubspec.yaml 中正确定义。

但是,当我尝试从资产 (JSON) 加载数据时,它只加载了一半的数据。

appData.json

{
"plan": [
    {
        "name": "plan_demo",
        "author": "A Name",
        "version": 1,
        "times_completed": 0,
        "percent_done": 0,
        "last_date_read": "1970-01-09",
        "missedDate": [
            {
                "date": "1970-01-01"
            },
            {
                "date": "1970-01-05"
            }
        ],
        "part": [
            {
                "isDone": false,
                "startBook": 1,
                "endBook": 1,
                "startChapter": 1,
                "endChapter": 1,
                "startVerse": 1,
                "endVerse": 5
            },
            {
                "isDone": false,
                "startBook": 1,
                "endBook": 1,
                "startChapter": 1,
                "endChapter": 1,
                "startVerse": 6,
                "endVerse": 10
            }
        ]
    },
    {
        "name": "another plan",
        "author": "Another Name",
        "version": 1,
        "times_completed": 0,
        "percent_done": 0,
        "last_date_read": "2020-12-28",
        "missedDate": [
            {
                "date": "2020-11-01"
            }
        ],
        "part": [
            {
                "isDone": false,
                "startBook": 1,
                "endBook": 1,
                "startChapter": 1,
                "endChapter": 1,
                "startVerse": 1,
                "endVerse": 5
            },
            {
                "isDone": false,
                "startBook": 1,
                "endBook": 1,
                "startChapter": 1,
                "endChapter": 1,
                "startVerse": 6,
                "endVerse": 10
            }
        ]
    }
  ]
}

调用方法如下:

@override
void initState() {
  super.initState();

  rootBundle.loadString("assets/appData.json").then((value) {
    print('rootBundle : ' + value);
  });
}

现在的输出是:

I/flutter (20595): rootBundle : {
I/flutter (20595):     "plan": [
I/flutter (20595):         {
I/flutter (20595):             "name": "plan_demo",
I/flutter (20595):             "author": "A Name",
I/flutter (20595):             "version": 1,
I/flutter (20595):             "times_completed": 0,
I/flutter (20595):             "percent_done": 0,
I/flutter (20595):             "last_date_read": "1970-01-09",
I/flutter (20595):             "missedDate": [
I/flutter (20595):                 {
I/flutter (20595):                     "date": "1970-01-01"
I/flutter (20595):                 },
I/flutter (20595):                 {
I/flutter (20595):                     "date": "1970-01-05"
I/flutter (20595):                 }
I/flutter (20595):             ],
I/flutter (20595):             "part": [
I/flutter (20595):                 {
I/flutter (20595):                     "isDone": false,
I/flutter (20595):                     "startBook": 1,
I/flutter (20595):                     "endBook": 1,
I/flutter (20595):                     "startChapter": 1,
I/flutter (20595):                     "endChapter": 1,
I/flutter (20595):                     "startVerse": 1,
I/flutter (20595):                     "endVerse": 5
I/flutter (20595):                 },
I/flutter (20595):                 {
I/flutter (20595):                     "isDone": false,
I/flutter (20595):                     "startBook": 1,
I/flutter (20595):                     "endBook": 1,
I/flutter (20595):                     "startChapter": 1,
I/flutter (20595):                     "endChapter": 1,
I/flutter (20595):                     "startVerse":

为什么我的数据在文件中的某个点后被截断?

【问题讨论】:

  • 打印语句只打印一定数量的字符......所以你的数据可能没问题。尝试将您的 json 存储在合适的变量中并打印其部分。
  • 有点真实;在虚拟 Android 设备上运行时,数据很好,但从打印命令中删除。在 Edge 中运行时,打印命令会输出所有数据。这对我来说有点奇怪。
  • 如果您的 json 长度为 1m 个字符,但不确定您是否想要全部打印出来。到目前为止,我对 print() 截断非常满意。 :-) 如果我需要更多,我通常使用一个类来定义它并准确打印我需要查看的位。

标签: flutter dart bundle


【解决方案1】:

按照@GrahamD 的链接,一个可能的解决方案正在使用

debugPrint(someSuperLongString, wrapWidth: 1024);

使用可选参数,输出不会被截断。使用正常的打印功能会发生截断,即使编辑器不像我的情况那样显示。

【讨论】:

    【解决方案2】:

    一个更好的解决方法可能是创建您自己的printsplits更小的块,然后您不需要为每个长的string 修改code。例如:

    void printWrapped(String text) {
      final pattern = RegExp('.{1,800}'); // 800 is the size of each chunk
      pattern.allMatches(text).forEach((match) => print(match.group(0)));
    }
    

    像这样使用它

    printWrapped("Your very long string ...");
    

    Credits 以及在颤振中打印长字符串的更多解决方案 See This 线程

    【讨论】:

      【解决方案3】:

      正如其他答案所提到的,问题是打印语句中的截断。讨论了很多方法来克服这个问题herehere。但是我想为您的情况提出一种不同的方法,我们可以通过从dart:developer 导入来使用inspect()。这增加了折叠并且类似于在浏览器的控制台中打印一个对象。 Link

      示例

      rootBundle.loadString("assets/appData.json").then((value) {
        inspect(jsonDecode(value));
      });
      

      这在这种情况下效果很好,因为使用折叠将整个 JSON 打印为单个字符串更为实用。

      【讨论】:

        猜你喜欢
        • 2023-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-28
        • 1970-01-01
        • 2013-05-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多