json - 如何在飞镖颤振中将json字符串转换为json对象?
问题描述
我有这样的字符串,
{id:1, name: lorem ipsum, address: dolor set amet}
我需要将该字符串转换为 json,我如何在 dart flutter 中做到这一点?非常感谢你的帮助。
解决方案
你必须使用json.decode
. 它接受一个 json 对象并让您处理嵌套的键值对。我给你写个例子
import 'dart:convert';
// actual data sent is {success: true, data:{token:'token'}}
final response = await client.post(url, body: reqBody);
// Notice how you have to call body from the response if you are using http to retrieve json
final body = json.decode(response.body);
// This is how you get success value out of the actual json
if (body['success']) {
//Token is nested inside data field so it goes one deeper.
final String token = body['data']['token'];
return {"success": true, "token": token};
}
推荐阅读
- typescript - 为什么这个程序在调用我的“then”子句之前就退出了?
- java - Java 正则表达式匹配比较运算符
- spring-boot - Spring Boot 2.3.0.M4、Cassandra 和 SSL
- symfony - Symfony prod 模式下无法识别的字段库
- sql - 显示输出是基于过滤条件的列
- azure-pipelines-release-pipeline - 如何通过 REST 方法获取发布管道的输出
- mongodb - Azure Cosmos DB Mongo - 资源令牌
- javascript - 如何为 Google 地图制定 Google Api 内容安全策略
- android - 如何使用 InMemoryDexClassLoader?
- php - 数组呈现为数字而不是值