Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to convert a dictionary to json string. However I am not getting quotes around any of the strings. I am using dart 2 . Here is what I have

  var resBody = {};
  resBody["email"] = "employerA@gmail.com";
  resBody["password"] = "admin123";
  var user = {};
  user["user"] = resBody;
  String str = json.encode(user);

Output is:

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

I would like this to be like an actual json object

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

How can I tell dart to put quotes around it ? I looked at this thread and am doing exactly what works for the user Am I doing something wrong ?

To me this looks like you are printing user (the dictionary) not json.encode(user) (the actual JSON). – Günter Zöchbauer May 2, 2018 at 7:53 That is not what I mean. What I mean is that you printed the dictionary, not the encoded JSON. – Günter Zöchbauer May 2, 2018 at 7:56 Yes, print the encoded JSON, not unencoded dictionary :D. Please add the print statement that you used to get the output shown in your question, then I might be able to make a concrete suggestion. – Günter Zöchbauer May 2, 2018 at 7:57 var resBody = {}; resBody["email"] = "employerA@gmail.com"; resBody["password"] = "admin123"; var user = {}; user["user"] = resBody; String str = json.encode(user); print(str);

prints

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

DartPad example

import 'dart:convert';
void main() {
  const JsonEncoder encoder = JsonEncoder.withIndent('  ');
  try {
  var resBody = {};
  resBody["email"] = "employerA@gmail.com";
  resBody["password"] = "admin123";
  var user = {};
  user["user"] = resBody;
  String str = encoder.convert(user);
  print(str);
  } catch(e) {
    print(e);

which gives you the beautified output

"user": { "email": "employerA@gmail.com", "password": "admin123" weird. it does the opposite for me. adds double quotes right away. i wanted it not to put double quotes. – chitgoks Dec 9, 2019 at 13:51 You shouldn't confuse what print(xxx) outputs with the actual value. If you print(someMap) it actually prints print(someMap.toString()) and toString() is what adds quotes for string keys. If you pass it as JSON, then toString() might not be called. – Günter Zöchbauer Dec 9, 2019 at 14:33 i got it to work in my test case. i had to use jsonencode for a list within a map. if its just the outer map i didnt need to jsonencode it when i passed it to http body. – chitgoks Dec 11, 2019 at 0:53 As far as I remember it sends a map as form data. That might not always be what you want. – Günter Zöchbauer Dec 11, 2019 at 3:58

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.