Home > Blockchain >  Flutter - Create Map from two JSON
Flutter - Create Map from two JSON

Time:12-28

I need to create a Map dictionary for this two JSON for creating a view like App Store/Play Store so, a view with cell grouped by different heading.

First JSON

[
   {
      "category_id":"1",
      "language_code":"it",
      "category_name":"punti d'interesse",
      "category_description":"descrizione",
      "is_hidden":"0"
   },
   {
      "category_id":"2",
      "language_code":"it",
      "category_name":"farmacie",
      "category_description":"desc farm",
      "is_hidden":"0"
   }
]

Second JSON

[
   {
      "poi_id":"1",
      "category_id":"1",
      "language_code":"it",
      "poi_name":"chiese",
      "poi_description":"",
      "poi_image":"church.jpg"
   },
   {
      "poi_id":"2",
      "category_id":"1",
      "language_code":"it",
      "poi_name":"monumenti",
      "poi_description":"",
      "poi_image":"monument.jpg"
   },
   {
      "poi_id":"3",
      "category_id":"2",
      "language_code":"it",
      "poi_name":"musei",
      "poi_description":"",
      "poi_image":"museum.jpg"
   }
]

How can I create?

CodePudding user response:

Here you go:

import 'dart:convert';

void main() {
  // Your first json
  List<Map<String, String>> map1 = [
    {
      "category_id": "1",
      "language_code": "it",
      "category_name": "punti d'interesse",
      "category_description": "descrizione",
      "is_hidden": "0"
    },
    {
      "category_id": "2",
      "language_code": "it",
      "category_name": "farmacie",
      "category_description": "desc farm",
      "is_hidden": "0"
    }
  ];
  // Your second json
  List<Map<String, String>> map2 = [
    {
      "poi_id": "1",
      "category_id": "1",
      "language_code": "it",
      "poi_name": "chiese",
      "poi_description": "",
      "poi_image": "church.jpg"
    },
    {
      "poi_id": "2",
      "category_id": "1",
      "language_code": "it",
      "poi_name": "monumenti",
      "poi_description": "",
      "poi_image": "monument.jpg"
    },
    {
      "poi_id": "3",
      "category_id": "2",
      "language_code": "it",
      "poi_name": "musei",
      "poi_description": "",
      "poi_image": "museum.jpg"
    }
  ];
  // The function that interests you (returns a map in the format you wanted)
  Map<String, List> convertData(List<Map> categories, List<Map> pois) {
    Map<String, List> result = {};
    for (var category in categories) {
      List<Map> poisOfCategory = [];
      for (var poi in pois) {
        if (category['category_id'] == poi['category_id']) {
          poisOfCategory.add(poi);
        }
      }
      result[category['category_name']] = poisOfCategory;
    }
    return result;
  }
  // Prints the map as a string
  print(json.encode(convertData(map1, map2)));
}

The output would be:

{
   "punti d'interesse":[
      {
         "poi_id":"1",
         "category_id":"1",
         "language_code":"it",
         "poi_name":"chiese",
         "poi_description":"",
         "poi_image":"church.jpg"
      },
      {
         "poi_id":"2",
         "category_id":"1",
         "language_code":"it",
         "poi_name":"monumenti",
         "poi_description":"",
         "poi_image":"monument.jpg"
      }
   ],
   "farmacie":[
      {
         "poi_id":"3",
         "category_id":"2",
         "language_code":"it",
         "poi_name":"musei",
         "poi_description":"",
         "poi_image":"museum.jpg"
      }
   ]
}
  • Related