json.decode和jsonDecode返回_JsonMap类型的对象

42fyovps  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(99)

我开始开发一个只支持web的flutter应用程序,并使用dart:convert库来解码json。当从服务器获取响应并通过json.decode对其进行解码时,它奇怪地变成了一个_JsonMap对象,其中包含_JsonMap对象。
下面是我在调试时在控制台中的输出:

json.decode('{"data":[{"_id":"64517a0cd447940df60e74c2", "type":["asd", "fds"]}]}')
json.decode('{"data":[{"_id":"64517a0cd447940df60e74c2"}]}')
json.decode('["foo", { "bar": 499 }]')

字符串
x1c 0d1x的数据
创建一个新项目可以解决这个问题,但我没有任何想法可以调试。

flutter doctor -v
[✓] Flutter (Channel stable, 3.10.6, on macOS 13.0.1 22A400 darwin-arm64, locale en-UA)
    • Flutter version 3.10.6 on channel stable at /Users/vladli/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision f468f3366c (4 weeks ago), 2023-07-12 15:19:05 -0700
    • Engine revision cdbeda788a
    • Dart version 3.0.6
    • DevTools version 2.23.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/vladli/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14C18
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] VS Code (version 1.80.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.70.0

[✓] Connected device (3 available)
    • iPhone 12 mini (mobile) • 1BB27A09-BBE8-44D9-977E-B9D56B962071 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-16-2 (simulator)
    • macOS (desktop)         • macos                                • darwin-arm64   • macOS 13.0.1 22A400 darwin-arm64
    • Chrome (web)            • chrome                               • web-javascript • Google Chrome 115.0.5790.114

[✓] Network resources
    • All expected network resources are available.

• No issues found!


问题是-为什么我会得到这个_JsonMap?看起来是内部的,网上几乎没有任何关于它的信息。

2q5ifsrm

2q5ifsrm1#

这不是你的代码的问题;它是jsonDecode的底层实现。
如果你要将一个对象转换为并打印它的类型,你确实会看到它是一个_jsonMap

import 'dart:convert';

void main() {
  print(jsonDecode(
          '{"data":[{"_id":"64517a0cd447940df60e74c2", "type":["asd", "fds"]}]}')
      .runtimeType);
}

字符串
印刷品:

_JsonMap


所以你是对的:看起来调用jsonDecode会返回一个Map,然而,实际上,它是一个_jsonMap
别忘了Dart是开源的。这样你就可以更深入地挖掘

相关问题