json - 如何从此json字段获取数据?

标签 json flutter dart

你好吗?

我正在尝试从json获取数据以在屏幕上显示,该图像应类似于下图。我可以获取大多数数据,除了一个编码为String的字段,其中包含图像和类似这样的描述:

"lessonText": "{\"ops\":[{\"insert\":{\"image\":\"data:image/jpeg;base64,(IMAGE CODE HERE)=\"}},{\"attributes\":{\"color\":\"#444444\"},\"insert\":\"(LESSON TEXT HERE)\"},{\"insert\":\"\\n\"}]}",

如何从这里提取数据?我试图将其转换为Map,但无法正常工作。

谢谢您的帮助!

Desired screen

最佳答案

与此相符的东西应该给你的印象

// json string containing the base64 image string
String jsonString = "{\"ops\":[{\"insert\":{\"image\":\"data:image/png;base64,(IMAGE CODE HERE)=\"}},{\"attributes\":{\"color\":\"#444444\"},\"insert\":\"(LESSON TEXT HERE)\"},{\"insert\":\"\\n\"}]}";

// convert the string to a map structure
Map<String, dynamic> json = jsonDecode(jsonString);

// extract the image string
String imageString = json['ops'][0]['insert']['image'];

// extract the base64 string
var prefix = "data:image/png;base64,";
var imageBase64String = imageString.substring(prefix.length);

// decode the base 64 string
var bytes = base64Decode(imageBase64String);

// build the image widget from bytes
var imageWidget = Image.memory(bytes);

关于json - 如何从此json字段获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62480513/

相关文章:

jquery - 使用 jQuery 对复杂对象使用 JSON 动态更新 html 表

ios - 如何打印json数据

android - 在 android 中发布 ion 解析 JSON 数据

dart - 在 flutter 中使用新关键字

flutter - RangeError(索引):无效值:有效值范围为空:0,Flutter

flutter - 如何使用flutter发送文件到node.js服务器?

php - 读取由 PHP 生成的包含 @attributes 的 JSON(使用 jQuery)

firebase - Firebase for Flutter 中此身份验证背后的逻辑是什么?

flutter - 测试小部件时,在 BlocListener 上测试失败

flutter - 文本校正功能