Skip to content

Instantly share code, notes, and snippets.

@p32929
Created April 12, 2024 00:03
Show Gist options
  • Save p32929/0a57c801666b0d76cffbf08eeb73e245 to your computer and use it in GitHub Desktop.
Save p32929/0a57c801666b0d76cffbf08eeb73e245 to your computer and use it in GitHub Desktop.
import 'dart:convert';
dynamic parseJson(String jsonString, String path) {
var json = jsonDecode(jsonString);
List<String> parts = path.split('.');
dynamic element = json;
for (var part in parts) {
if (part.contains('[') && part.contains(']')) {
var indexStart = part.indexOf('[');
var indexEnd = part.indexOf(']');
String key = part.substring(0, indexStart);
int index = int.parse(part.substring(indexStart + 1, indexEnd));
element = element[key][index];
} else {
element = element[part];
}
}
return element;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment