我有这样的字符串,
{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};
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句