Home > front end >  Get length of nested JSON property in Flutter
Get length of nested JSON property in Flutter

Time:10-31

The dart code below accesses nested JSON properties by using json.decode(), but there does not seem to be a property for count, length, or size.

Is it possible to get the length of a nested JSON property in Flutter?

JSON Response:

{
    "obj1":{
        "list1":[
            {"key1":"value1"},
            {"key2":"value2"},
            {"key3":"value3"}
        ]
    }
}

Dart code:

var response = await httpRequest.response;
Map<String, dynamic> responseJson = json.decode(response.data);
print(responseJson['obj1']['list1']); // How to get length?

CodePudding user response:

You can get the length of the map with the length property. It will give you the number of pairs on the map (or submap):

    print(responseJson['obj1'].length);
    result: 1
    print(responseJson['obj1']['list1'].length);   
    result 3
  • Related