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 ?
–
–
–
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"
–
–
–
–
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.