我正在尝试将字典转换为json字符串。但是我没有得到任何字符串的引号。我正在使用飞镖2。这就是我所拥有的
var resBody = {};
resBody["email"] = "employerA@gmail.com";
resBody["password"] = "admin123";
var user = {};
user["user"] = resBody;
String str = json.encode(user);
输出是:
{user: {email: employerA@gmail.com, password: admin123}}
我希望这就像一个真正的json对象
{"user": {"email": "employerA@gmail.com", "password: admin123"}}
我怎么能告诉飞镖在它周围加上引号? 我查看了this线程,并且正在完成对用户有用的功能 我做错了吗?
答案 0 :(得分:11)
这是按预期工作的
Invoke-WebRequest -Uri 'http://localhost:3000/api/add/user' -Method POST -ContentType 'application/json' -Body (ConvertTo-Json $postParams -Compress)
打印
import 'dart:convert';
void main() {
var resBody = {};
resBody["email"] = "employerA@gmail.com";
resBody["password"] = "admin123";
var user = {};
user["user"] = resBody;
String str = json.encode(user);
print(str);
}
答案 1 :(得分:0)
如果响应内容太长,那么So below是一个功能,也可以帮助将长而精确的JSON数据打印到您的Dart终端中。可以像在Dart中使用print('some message')
一样使用此函数,但是参数必须是JSON字符串。
import 'dart:convert'; //Don't forget to import this
void printJson(String input) {
const JsonDecoder decoder = JsonDecoder();
const JsonEncoder encoder = JsonEncoder.withIndent(' ');
final dynamic object = decoder.convert(input);
final dynamic prettyString = encoder.convert(object);
prettyString.split('\n').forEach((dynamic element) => print(element));
}
您会使用/称呼它
Future<dynamic> getYourApiData({double lon, double lat}) async {
final String url = yourApiUrl;
final http.Response response = await http.get(url);
response = {
"user": {"email": "employerA@gmail.com", "password": "admin123"}
}; //Suppose your response is like this
/*
Below we are calling the printJson() with the response.body
But you need to make sure it's in String format.
*/
printJson(response.body.toString());
}
它的输出将是这样