how to convert json string in dart flutter?

how to convert json string in dart flutter?

Problem Description:

I have string response like this, I got only below response of my api.

{authToken: msadnmsandnasdn}

and I have to convert as below.

{"authToken": "msadnmsandnasdn"}

So how i can do this please Help me.

Solution – 1

You can use various manipulation operations to do that manually:

import 'dart:convert';
void main() {
  var s = "{authToken: msadnmsandnasdn, name:risheek}";
  var kv = s.substring(0,s.length-1).substring(1).split(",");
  final Map<String, String> pairs = {};
  for (int i=0; i < kv.length;i++){
    var thisKV = kv[i].split(":");
    pairs[thisKV[0]] =thisKV[1].trim();
  var encoded = json.encode(pairs);


{"authToken":"msadnmsandnasdn"," name":"risheek"}

Solution – 2

You need to use jsonDecode on that string like this:

var response = {authToken: msadnmsandnasdn....};
var result = jsonDecode(response);
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.