skip to Main Content

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.

2

Answers


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

    Output:

    {"authToken":"msadnmsandnasdn"," name":"risheek"}
    
    Login or Signup to reply.
  2. You need to use jsonDecode on that string like this:

    var response = {authToken: msadnmsandnasdn....};
    var result = jsonDecode(response);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search