How to convert Json String to HashMap?

Viewed 90

I am new to flutter and I want to convert Json string to hash map. I searched google and tried in differnt ways. But unable to get the correct answer. Josn String = "{\"key\":\"key\", \"name\":\"Kumar\", \"status\":999, \"Key\":\"key\", \"key\":\"key\", \"number\":999988, \"object\":{\"text\":\"text\", \"text\":\"text!\", \"status\":1,\"text\":\"text\", \"Url\":null } }"

var url = URL;
final responseResult = await http
    .post(url, body: {'EmailID': 'm@m.com', 'Password': 'bmbmjbmjbjhty67rr7'});

print(responseResult.body);

final body = json.decode(responseResult.body) as HashMap;

print(body['UserName']);

This is what I tried.

Thanks in Advance,

1 Answers

First escape characters need to be removed, to remove this wrap string inside

 r'''Sttring'''

Example:

import 'dart:convert';

void main() {
  var jsonData = r'''"{\"AccessToken\":\"tokenkey\", \"UserName\":\"Kumar\", \"UserStatus\":999, \"UserAccessKey\":\"userAccessKey\", \"DeviceAccessKey\":\"Device Access key\", \"UserID\":999988,  \"ObjResponse\":{\"ResponseHeaderText\":\"success\", \"ResponseBodyText\":\"Success!\", \"ResponseCode\":1,\"ResponseMethod\":\"Authentication_Success\", \"Url\":null } }"''';
  var responseResult = json.decode(json.decode(jsonData));

 print(responseResult['UserName']);
}

Output:

Kumar
Related