Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert map to string?

Tags:

flutter

dart

my obj is:

Map myJSON = {
  "name": {"first":"foo","last":"bar"}, 
  "age":31, "city":"New York"
}; 

I want to replace it with the new output Ex:

String myJSON = '{"name":{"first":"foo","last":"bar"}, "age":31, "city":"New York"}';
like image 906
Jagadeesh Avatar asked Sep 06 '19 11:09

Jagadeesh


People also ask

How do you convert a Map key and value in a string?

Using toString() method The string representation of a map consists of a list of key-value pairs enclosed within curly braces, where the adjacent pairs are delimited by a comma followed by a single space and each key-value pair is separated by the equals sign ( = ). i.e., {K1=V1, K2=V2, ..., Kn=Vn} .

Can we use Map for string?

map() is known to belong to the array prototype. In this step you will use it to convert a string to an array. You are not developing the method to work for strings here.


2 Answers

Use the convert class

import 'dart:convert';

Convert string to Map

json.decode(stringData);

Convert Map to string

json.encode(mapData);
like image 161
Billy Mahmood Avatar answered Sep 26 '22 00:09

Billy Mahmood


  String jsonString = jsonEncode(myJSON);
  print(jsonString);
like image 29
mirkancal Avatar answered Sep 27 '22 00:09

mirkancal