简体   繁体   中英

Nested map json parsing in flutter

Input string:- '{"data":{"K1":{"K2":{"k3":true}}}}'

Output string:- "data/K1/K2"

I want json parsing for above input and output. It will be a great help if anyone can help me out. Thank you in advance. I am attaching my code here.

import 'dart:convert';

void iterateJson(String jsonStr) {
  Map<String, dynamic> myMap = json.decode(jsonStr);
  List<dynamic> data = myMap["K1"][0];
  data.forEach((a) {
    (a as Map<String, dynamic>).forEach((key, value) {
      print(key);
    });
  });
}
void main(){
  iterateJson('{"data":{"K1":{"K2":{"k3":true}}}}');
}
import 'dart:convert';

void iterateJson(String jsonStr) {
  Map<String, dynamic> myMap = json.decode(jsonStr);
  String result = '';

  try{
    while(myMap.entries.isNotEmpty){    
      result += '/'+ myMap.entries.first.key;
      myMap = myMap.entries.first.value;
    }
  }catch(e){
//    print('reached end of data');
  }
  
  print(result.substring(1, result.length));


}
void main(){
  iterateJson('{"data":{"K1":{"K2":{"k3":true}}}}');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM