Home > Back-end >  how to get JSON nested data
how to get JSON nested data

Time:10-18

Hope you all are doing well, I need some help please, I am developing an app using JSON file and I want to search for a value in that file, I did good so far, but what I couldn't understand is that when I search for some value it prints, other no and throw an error, Here is my JSON file Stucture:

[ 
    { "Category": "Brain",
         "Value": [
            ] },
    {     "Category": "NODE",
         "Value": [ ]}  ]

As you can see, the file includes category and value, and each value is a list

  {
    "ErrorType": "backendToBrain",
    "ErrorList": [
      {
        "HexCode": "0x1100",
        "DecCode": 4352,
        "reason": ""
      },
   
      {
        "HexCode": "0x1102",
        "DeCode": 4354,
        "reason": "File error"
      },
      {
        "HexCode": "0x1103",
        "DeCode": 4355,
        "reason": "GNSS error"
      },
      {
        "HexCode": "0x1104",
        "DeCode": 4352,
        "reason": "IP error"
      },
   
    ]
  },
  {
    "ErrorType": "userToBrain",
    "ErrorList": [
      {
        "HexCode": "0x1001",
        "DecCode": 4097,
        "reason": "Memory Error, Cycle Req"
      },
    
      {
        "HexCode": "0x1005",
        "DecCode": 4101,
        "reason": "Node not available"
      }
    ]
  },

  {
    "ErrorType": "BLEToBrain",
    "ErrorList": [
      {
        "HexCode": "0x1200",
        "DeCode": 4608,
        "reason": ""
      },

      {
        "HexCode": "0x1203",
        "DeCode": 4611,
        "reason": "Error initialising security"
      },
   
    
    ]
  }

and each list contains a value "ErrorType" and a list of that error, so I want to search for a specific data, I wrote this method

  static List<CodeReasonObj> searchForError(data, BrainErrorType brainErrorType) {
List<CodeReasonObj> g = [];
for (int index = 0; index < data.length; index  ) {
  if (data[index]['Category'] == 'Brain') {
    for (int i = 0; i < data[index]['Value'].length; i  ) {
      if (data[index]['Value'][i]['ErrorType'] ==
          brainErrorType.toString()) {
        g = List.generate(
            data[index]['Value'][i]['ErrorList'].length,
                (j) =>
                CodeReasonObj(
                    HexCode: data[index]['Value'][i]['ErrorList'][j]['HexCode'],
                    DecCode: data[index]['Value'][i]['ErrorList'][j]['DecCode'],
                    reason: data[index]['Value'][i]['ErrorList'][j]['reason'])
        );
        for (int i = 0; i < g.length; i  ) {
          print(g[i].HexCode);
        }
      }
    }
  }
}
return g;

}

when the brain is equals to userToBrain, it works fine and return the list, when it is another value, it does not work

here is a link to my JSON file : https://drive.google.com/file/d/1JAtELt0VQBCYLrqBY03Ym5BSqZeigE8Z/view?usp=sharing

CodePudding user response:

I believe the error is most likely because the JSON has typos in it. At many places where it's supposed to say

DecCode

it actually says

DeCode

CodePudding user response:

If your JSON structure is true, you can paste your JSON in the link below, click convert and get your Dart classes for free.

  • Related