Created
April 12, 2024 00:03
-
-
Save p32929/0a57c801666b0d76cffbf08eeb73e245 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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