Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart json.encode returns json string with key values without quotes

Tags:

flutter

dart

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"] = "[email protected]";   resBody["password"] = "admin123";   var user = {};   user["user"] = resBody;   String str = json.encode(user); 

Output is:

{user: {email: [email protected], password: admin123}} 

I would like this to be like an actual json object

{"user": {"email": "[email protected]", "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 ?

like image 715
MistyD Avatar asked May 02 '18 07:05

MistyD


People also ask

Do keys in JSON need quotes?

JavaScript object literals do not require quotes around a key name if the key is a valid identifier and not a reserved word. However, JSON always requires quotes around key names.

What does JSON encode do in dart?

jsonEncode function Null safetyConverts object to a JSON string. If value contains objects that are not directly encodable to a JSON string (a value that is not a number, boolean, string, null, list or a map with string keys), the toEncodable function is used to convert it to an object that must be directly encodable.

Can JSON strings have single quotes?

Strings in JSON are specified using double quotes, i.e., " . If the strings are enclosed using single quotes, then the JSON is an invalid JSON .


2 Answers

This is working as expected

import 'dart:convert';  void main() {   var resBody = {};   resBody["email"] = "[email protected]";   resBody["password"] = "admin123";   var user = {};   user["user"] = resBody;   String str = json.encode(user);   print(str); } 

prints

{"user":{"email":"[email protected]","password":"admin123"}} 

DartPad example

[or]

import 'dart:convert';  void main() {   const JsonEncoder encoder = JsonEncoder.withIndent('  ');   try {   var resBody = {};   resBody["email"] = "[email protected]";   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": "[email protected]",     "password": "admin123"   } } 
like image 65
Günter Zöchbauer Avatar answered Oct 09 '22 07:10

Günter Zöchbauer


If the response content is too long, So Below is a function to help also print long and exact formation JSON data into your Dart terminal. This function can be used just like you would with a print('some message') in Dart, but the parameter has to be a String of 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)); } 

You would use/call it like

  Future<dynamic> getYourApiData({double lon, double lat}) async {     final String url = yourApiUrl;     final http.Response response = await http.get(url);     response = {       "user": {"email": "[email protected]", "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());   } 

The output of this will be something like this

enter image description here

like image 36
Paresh Mangukiya Avatar answered Oct 09 '22 06:10

Paresh Mangukiya