0

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 2

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"}
Sign up to request clarification or add additional context in comments.

3 Comments

msadnmsandnasdn.... means i got only token nothing else, i can edit the question.
yeah yeah just insert your string as var s and use it however you want it'll work the same
can you upvote as well?
1

You need to use jsonDecode on that string like this:

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

1 Comment

i tried you code but it returns "Uncaught Error: FormatException: SyntaxError: Expected property name or '}' in JSON at position 1 "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.