'Get string after array of characters
I got a string like this:
"{cat: Molly, dog: Feefee}"
I want save in a String cat name, and in different String dog name. How can I do it?
I tried manipulating with constant indexes, but I wnt to be sure, the name will be saved properly when there are different names
Solution 1:[1]
Maybe you're looking for it?
import 'dart:convert';
void main() {
final a = '{"cat": "Molly", "dog": "Feefee"}';
final b = json.decode(a);
final map = Map<String, dynamic>.from(b);
print(map['cat']);
print(map['dog']);
}
Output:
Molly
Feefee
Solution 2:[2]
I would suggest doing some data cleaning so that your data comes out in JSON format. Then you can decode that and convert it to a map. However, if that is not possible. Here is a round a bout way of getting the values you want.
String example = "{cat: Molly, dog: Feefee}";
var re = RegExp(r'(?<={)(.*)(?=})');
var match = re.firstMatch(example)?.group(0);
var splitted = match?.split(",");
var cat = splitted?[0].split(":")[1];
print(cat); //prints Molly
Solution 3:[3]
you can create model from your json. https://javiercbk.github.io/json_to_dart just copy paste your json, it will auto generate your model
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Arnas |
| Solution 2 | Tray Denney |
| Solution 3 | anggadaz |
