Home > front end >  how to convert json string in dart flutter?
how to convert json string in dart flutter?

Time:12-06

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.

CodePudding user response:

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);
  print(encoded);
}

Output:

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

CodePudding user response:

You need to use jsonDecode on that string like this:

var response = {authToken: msadnmsandnasdn....};
var result = jsonDecode(response);
  • Related