0

"

success": true, 
     "result": {
        "values": {
            "asdf": [],
            "dj": [
                {
                    "id": 18,
                    "ownerId": "5b0b3932-e262-4ac4-923c-13daf2bd4a3c",
                    "ownerName": "tester",
                    "name": "masr",
                    "description": "she was and firebase have also had to make their decision and make the beg to 8be 8the 8same 6th 7century 8of 8and 6and 88th century ones in flutter take on my favourite ",
                    "statusId": "PENDING",
                    "status": null,
                    "price": 9000.00,
                    "isPublic": false,
                    "startDate": "2022-05-26T00:00:00",
                    "expectedEndDate": "2022-05-27T00:00:00",
                    "finishDate": null,
                    "interests": [
                        {
                            "id": "my-first-interest",
                            "isDeleted": false
                        },
                        {
                            "id": "gdg",
                            "isDeleted": false
                        },
                        {
                            "id": "dj",
                            "isDeleted": false
                        }
                    ]
                },
      ] 
    }
     }

the dynamic key is asdf and dj change form user to anoter i want to get id or ownername .... etc without object can any one help me in this cause

1 Answer 1

1

Since you don't know the key name in advance, you will have to iterate over all of them looking for JSON object members that look like they contain ID and owner. Something like this:

import 'dart:convert';
import 'dart:io';

void main() {
  final decoded = json.decode(File('json1.json').readAsStringSync());

  // values will be a JSON object
  final values = decoded['result']['values'] as Map<String, dynamic>;

  // values.values will be all of the JSON arrays in that object
  // do a whereType just to rule out any other fields in the JSON object
  // use expand to merge all lists together
  // and wheretype again to double check that we only have JSON objects
  // further check that only JSON objects with the right values are selected
  // and map these to PODOs
  final result = values.values
      .whereType<List>()
      .expand((e) => e)
      .whereType<Map>()
      .where((e) => e.containsKey('id') && e.containsKey('ownerId'))
      .map<IdAndOwner>((m) => IdAndOwner(m['id'], m['ownerId']))
      .toList();
  print(result); // prints [Id/OwnerId=18/5b0b3932-e262-4ac4-923c-13daf2bd4a3c]
}

class IdAndOwner {
  final int id;
  final String ownerId;

  IdAndOwner(this.id, this.ownerId);

  @override
  String toString() => 'Id/OwnerId=$id/$ownerId';
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your answer , i understood your solution but it has one limitation , i need to display each list under each category as what we see in udemy [ recommendations for each category] example : i got 3 categories [programming ,marketing ,finance ] i want to display each course/Room under its category that came in the json , i think the it will a kind of Map<String,List<Room>> but i couldn't figure it out
Change values.values to values.entries so that you have access to the key name asdf - presumably it's more meaningful than that? Remove the expand (and the whereTypes - assuming you know you are getting the right types - it's not possible to tell from your JSON snippet). Then map your entries into whatever key-values you want. Experiment and figure it out; you have the benefit of access to the real data and a debugger!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.