Reputation: 159
In a project I get a String, I want convert in a correct json. I have test with json.encode()
but not work.
Thanks,
String not json:
{id: 2, status: 2, pseudo: giles, password: lmstvixKUKcmEKL, email: [email protected], phone: 710-934-8381, birthday: 2022-03-26, resume: Velit unde maiores nostrum sit et voluptate perspiciatis repellat., avatar_path: avatar_8.png, active: 0, created_at: 2022-05-26 13:40:31, updated_at: 2022-05-28 13:04:44, age: 0, avatar: http://localhost/api_alligatorsdujeu/storage/app/gallery/avatars/avatar_8.png, family: {id: 8, name: TREMBLAY, newsletter: false, philibert: true, active: true, created_at: 2022-05-26 10:18:15.000, updated_at: 2022-05-26 10:18:15.000}}
String json :
{"id": 2, "status": 2, "pseudo": "giles", "password": "lmstvixKUKcmEKL", "email": "[email protected]", "phone": "710-934-8381", "birthday": "2022-03-26", "resume": "Velit unde maiores nostrum sit et voluptate perspiciatis repellat.", "avatar_path": "avatar_8.png", "active": 0, "created_at": "2022-05-26 13:40:31", "updated_at": "2022-05-28 13:04:44", "age": 0, "avatar": "http://localhost/api_alligatorsdujeu/storage/app/gallery/avatars/avatar_8.png", "family": {"id": 8, "name": "TREMBLAY", "newsletter": false, "philibert": true, "active": true, "created_at": "2022-05-26 10:18:15.000", "updated_at": "2022-05-26 10:18:15.000"}}
Upvotes: 0
Views: 1791
Reputation: 119
You're trying to convert a string to a json that doesn't match the json syntax, so any attempt in using the usual json methods will fail.
Your little trick is fine but I would suggest using a regular expression like so :
import 'dart:convert';
void main() {
String notJson = '{id: 2, status: 2, pseudo: giles, password: lmstvixKUKcmEKL, email: [email protected], phone: 710-934-8381, birthday: 2022-03-26, resume: Velit unde maiores nostrum sit et voluptate perspiciatis repellat., avatar_path: avatar_8.png, active: 0, created_at: 2022-05-26 13:40:31, updated_at: 2022-05-28 13:04:44, age: 0, avatar: http://localhost/api_alligatorsdujeu/storage/app/gallery/avatars/avatar_8.png, family: {id: 8, name: TREMBLAY, newsletter: false, philibert: true, active: true, created_at: 2022-05-26 10:18:15.000, updated_at: 2022-05-26 10:18:15.000}}';
String isJson = notJson.replaceAllMapped(RegExp(r'(?<=\{| )\w(.*?)(?=\: |, |})'), (match) {
return '"${match.group(0)!}"';
});
print(isJson);
}
Might be subject to modifications if used on a more complex case to parse.
Upvotes: 0
Reputation: 159
Update : I found a solution even if it is not optimized.
String convertToJson(String str) {
str = str.toString().replaceAll('{', '{"');
str = str.toString().replaceAll(': ', '": "');
str = str.toString().replaceAll(', ', '", "');
str = str.toString().replaceAll('}', '"}');
return str;
}
Upvotes: 1
Reputation: 1792
Try jsonEncode(jsonDecode(yourString));
Use jsonDecode(string)
this will return you a Flutter Map
if the string is valid
After that,
use jsonEncode(theResultMap)
to return it to string with double quotes
Upvotes: 0