jsonflutterdartjsonparser

Parsing dynamic json values in Flutter


In my flutter application, I am receiving json response and can also able to parse it easily. But At a point or a key named "data" in json response giving me dynamic value as below:

Sometime it coming like (1):

"data": {
                "count": "237",
                "dollars": "279370.62"
            },

Sometime it coming like (2):

"data": 122093.43,

Sometime it coming like (3):

"data": {
                "colas": 4,
                "formulas": 0
            },

and Sometime it coming like (4):

"data": "122093.43",

So How can I decide that whether is it a normal String value inside json or I need to parse a data object? and you might also noticed that the keys inside data is also getting changed.

What is the best way to handle this?


Solution

  • With the use of type dynamic we can solve this issue and then using JsonDecode functionality its easy to get values for the required json keys.