I used to parse JSON to list of objects by the following way:
final response = await api(map);
var raw = jsonDecode(response.body)[0];
dataList = parseJson(raw["datalist"]);
List<dataRecord> parseJson(String responseBody) {
final parsed = convert.jsonDecode(responseBody).cast<Map<String, dynamic>>();
return parsed.map<dataRecord>((json) => dataRecord.fromJson(json)).toList();
}
and the JSON is in this format:
datalist: [{"id": 29737, "rent": 700, "unit": "A", .....
this works fine in all of my existing projects.
However, I am now retrieving data from an external source and the JSON is in this format:
data: [{id: 4109486, unicorn_id: 7836045, memorial_id: 23072601320076, ...
field labels and string values all without quotes, and I am not able to use the same parseJson
function to parse the JSON.
2
Answers
Use this method to add quotes around the keys and string values in the response.
One possible way is to add a hook to the JSON parser.
First hook to parse
_keyValue
.The second hook is to parse
_string
.This allows you to parse regular JSON and JSON that uses identifiers as object keys.
Output: