vIvId_gOat
vIvId_gOat

Reputation: 378

string passing error using json.decode in flutter

I'm saving a string of this format "[{text: drding4disruption.com, quantity:10.0}, {text: google.com, quantity:6.0}]"

so reading some tutorials I got to know that we can use json.convert method to parse a string but here i'm getting

I did try to use json.decode(the_string); but It shows this error

FormatException: Unexpected character (at character 3)
E/flutter (14216): [{text: 4 medium size potatoes (boiled, peeled), quantity: 4.0, measure: <u...
E/flutter (14216):   ^
E/flutter (14216): 

how do I parse it so that I can access the map as well

Upvotes: 0

Views: 231

Answers (2)

Jhakiz
Jhakiz

Reputation: 1609

Save the string in this format by adding double quote to fields and string values :

[
   {
      "text":"drding4disruption.com",
      "quantity":10
   },
   {
      "text":"google.com",
      "quantity":6
   }
]

Finally decode it: json.decode(the_string);

To consume it as YAML syntax take a look to this yaml package

Upvotes: 1

dm_tr
dm_tr

Reputation: 4783

Use the import 'dart:convert' library and do it as following:
From JSON list to String

final String listAsString = jsonEncode(list);

From String to JSON list, simply reverse

final List list = jsonDecode(listAsString);

Make sure your JSON map keys and values are quoted like this

var list = [{"text": "drding4disruption.com", "quantity":10.0}, {"text": "google.com", "quantity":6.0}];

Upvotes: 0

Related Questions