如何在dart flutter中将json字符串转换为json对象? [英] how to convert json string to json object in dart flutter?
本文介绍了如何在dart flutter中将json字符串转换为json对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的字符串,
{id:1, name: lorem ipsum, address: dolor set amet}
我需要将该字符串转换为json,如何在dart flutter中实现呢?非常感谢您的帮助.
And I need to convert that string to json, how I can do it in dart flutter? thank you so much for your help.
推荐答案
您必须使用 json.decode
.它接受一个json对象,并让您处理嵌套的键值对.我给你写个例子
You have to use json.decode
. It takes in a json object and let you handle the nested key value pairs. I'll write you an example
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};
}
这篇关于如何在dart flutter中将json字符串转换为json对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文