json - 如何从此 json 字段中获取数据?
问题描述
你好吗?
我正在尝试从 json 获取数据以显示在应该如下图所示的屏幕上。我能够获取大部分数据,除了一个编码为字符串的字段,它由图像和这样的描述组成:
"lessonText": "{\"ops\":[{\"insert\":{\"image\":\"data:image/jpeg;base64,(IMAGE CODE HERE)=\"}},{\"attributes\":{\"color\":\"#444444\"},\"insert\":\"(LESSON TEXT HERE)\"},{\"insert\":\"\\n\"}]}",
如何从这里提取数据?我试图将其转换为地图,但它不起作用。
谢谢您的帮助!
解决方案
与此相符的东西应该会给您图像
// 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);
推荐阅读
- angular - 检查数组 angular 6 中是否有部分刺痛
- azure-devops - 如何将 .gitIgnore 中的配置文件添加到 azure 管道构建?
- jquery - 如果未选择任何内容,则自动对焦于多选下拉列表
- javascript - 检查用户是否从使用快速护照后端的 React 登录
- .net - Powershell脚本访问本地文件而不是网页
- sql-server - 如何每天将生产 MSSQL 数据库复制到测试数据库
- ios - 我的 url 不返回 nil,而是在 swift 中返回空字符串
- c# - 找不到 appbundles 二进制文件 (Design Automation V3)
- ember.js - 如何从 EmberJS 中的另一个 REST url 检索相同的模型
- django - 有没有更好的方法在 admin.py 中显示一长串条目