Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart json.encode返回带有键值且不带引号的json字符串_Dart_Flutter - Fatal编程技术网

Dart json.encode返回带有键值且不带引号的json字符串

Dart json.encode返回带有键值且不带引号的json字符串,dart,flutter,Dart,Flutter,我正在尝试将字典转换为json字符串。但是,我没有得到任何字符串的引号。我用的是飞镖2。这是我的 var resBody = {}; resBody["email"] = "employerA@gmail.com"; resBody["password"] = "admin123"; var user = {}; user["user"] = resBody; String str = json.encode(user); 输出为: {user: {email: emp

我正在尝试将字典转换为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"}}
我怎么能告诉dart在它周围加引号? 我查看了线程,并且正在做适合用户的事情
我做错什么了吗?

这正按预期工作

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);
}
印刷品

{"user":{"email":"employerA@gmail.com","password":"admin123"}}

[或]

这将为您提供美化的输出

{
  "user": {
    "email": "employerA@gmail.com",
    "password": "admin123"
  }
}

如果响应内容太长,那么下面是一个函数,可以帮助您将长而准确的JSON格式数据打印到Dart终端中。此函数可以像在Dart中打印('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());
  }
Future getYourApiData({double lon,double lat})异步{
最后一个字符串url=yourApiUrl;
final http.Response Response=wait http.get(url);
答复={
用户:{“电子邮件”:employerA@gmail.com,“密码”:“admin123”}
}假设你的回答是这样的
/*
下面我们用response.body调用printJson()
但是你需要确保它是字符串格式的。
*/
printJson(response.body.toString());
}
这个的输出是这样的


对我来说,这看起来像是你在打印
user
(字典)而不是
json.encode(user)
(实际的json)。是的,user对象有一个键“user”,其中包含的对象resbody不是我的意思。我的意思是,你打印的是字典,而不是编码的JSON。关于如何修复它,有什么建议吗?是的,打印编码的JSON,而不是未编码的字典:D。请添加用于获取问题中显示的输出的打印语句,然后我可能能够提出具体建议。奇怪。对我来说正好相反。立即添加双引号。我不想让它加双引号。你不应该把输出的内容与实际值混淆。如果您
print(someMap)
它实际上会打印
print(someMap.toString())
,而
toString()
就是为字符串键添加引号的。如果您将其作为JSON传递,那么可能不会调用
toString()
。我让它在我的测试用例中工作。我必须使用JSONECode在地图中创建一个列表。如果它只是一个外部映射,那么当我将它传递给http主体时,我不需要对它进行JSONE编码。这可能并不总是您想要的。我使用json.decode(res.body))在print()中输出repsonse body。
  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());
  }