Home > Software design >  How can I extract values from a complicated string in Dart/Flutter?
How can I extract values from a complicated string in Dart/Flutter?

Time:09-01

I receive the following String from an API call in Flutter:

"meta_data": [
{
"id": 19293,
"key": "food_meta",
"value": "{\"extra_options\":{\"up to 8 Add-Ons (optional)\":[{\"id\":1274,\"price\":\"35,00kr\",\"price_float\":35,\"category\":\"up to 8 Add-Ons (optional)\",\"name\":\"EKSTRA PITABR\\u00d8D\",\"hide_prices\":false},{\"id\":1275,\"price\":\"45,00kr\",\"price_float\":45,\"category\":\"up to 8 Add-Ons (optional)\",\"name\":\"EKSTRA FETA OST\",\"hide_prices\":false},{\"id\":1276,\"price\":\"60,00kr\",\"price_float\":60,\"category\":\"up to 8 Add-Ons (optional)\",\"name\":\"EKSTRA KJ\\u00d8TT\",\"hide_prices\":false}]},\"additional_comments\":\"Si addon si comentariu\",\"extra_options_price\":140,\"original_price\":\"165\"}",
"display_key": "woofood_meta",
"display_value": "{\"extra_options\":{\"up to 8 Add-Ons (optional)\":[{\"id\":1274,\"price\":\"35,00kr\",\"price_float\":35,\"category\":\"up to 8 Add-Ons (optional)\",\"name\":\"EKSTRA PITABR\\u00d8D\",\"hide_prices\":false},{\"id\":1275,\"price\":\"45,00kr\",\"price_float\":45,\"category\":\"up to 8 Add-Ons (optional)\",\"name\":\"EKSTRA FETA OST\",\"hide_prices\":false},{\"id\":1276,\"price\":\"60,00kr\",\"price_float\":60,\"category\":\"up to 8 Add-Ons (optional)\",\"name\":\"EKSTRA KJ\\u00d8TT\",\"hide_prices\":false}]},\"additional_comments\":\"Si addon si comentariu\",\"extra_options_price\":140,\"original_price\":\"165\"}"
}
]

Is there any way to get only the values where the key=name?

like: EKSTRA FETA OST, EKSTRA PITABR\u00d8D\ etc.

This is where I got so far

 void main(){

String str = '{\"extra_options\":{\"up to 8 Add-Ons (optional)\":[{\"id\":1274,\"price\":\"35,00kr\",\"price_float\":35,\"category\":\"up to 8 Add-Ons (optional)\",\"name\":\"EKSTRA PITABR\\u00d8D\",\"hide_prices\":false},{\"id\":1275,\"price\":\"45,00kr\",\"price_float\":45,\"category\":\"up to 8 Add-Ons (optional)\",\"name\":\"EKSTRA FETA OST\",\"hide_prices\":false},{\"id\":1276,\"price\":\"60,00kr\",\"price_float\":60,\"category\":\"up to 8 Add-Ons (optional)\",\"name\":\"EKSTRA KJ\\u00d8TT\",\"hide_prices\":false}]},\"additional_comments\":\"Si addon si comentariu\",\"extra_options_price\":140,\"original_price\":\"165\"};';
 
String result = str.replaceAll('{', '').replaceAll('[', '').replaceAll('"extra_options":"up to 8 Add-Ons (optional)":', '').replaceAll('}', '').replaceAll(']', '');
   
List<String> extras = result.split(' ');

print(extras);

}

CodePudding user response:

In this scenario Regex is the best option Please check below code

void main() {
   String str = '{\"extra_options\":{\"up to 8 Add-Ons (optional)\":[{\"id\":1274,\"price\":\"35,00kr\",\"price_float\":35,\"category\":\"up to 8 Add-Ons (optional)\",\"name\":\"EKSTRA PITABR\\u00d8D\",\"hide_prices\":false},{\"id\":1275,\"price\":\"45,00kr\",\"price_float\":45,\"category\":\"up to 8 Add-Ons (optional)\",\"name\":\"EKSTRA FETA OST\",\"hide_prices\":false},{\"id\":1276,\"price\":\"60,00kr\",\"price_float\":60,\"category\":\"up to 8 Add-Ons (optional)\",\"name\":\"EKSTRA KJ\\u00d8TT\",\"hide_prices\":false}]},\"additional_comments\":\"Si addon si comentariu\",\"extra_options_price\":140,\"original_price\":\"165\"};';
    
  RegExp exp = new RegExp(r'\"name\":(.*?),');

  Iterable<RegExpMatch> matches = exp.allMatches(str);
  
  for(var t in matches){
    print(t.group(1));
  }
}
  • Related