希望你們一切都好,我需要一些幫助,我正在使用 JSON 檔案開發一個應用程式,我想在該檔案中搜索一個值,到目前為止我做得很好,但我無法理解的是,當我搜索它列印的一些值,其他沒有并拋出錯誤,這是我的 JSON 檔案結構:
[
{ "Category": "Brain",
"Value": [
] },
{ "Category": "NODE",
"Value": [ ]} ]
可以看到,該檔案包括category和value,每個value都是一個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"
},
]
}
每個串列都包含一個值“ErrorType”和該錯誤的串列,所以我想搜索一個特定的資料,我寫了這個方法
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;
}
當brain等于userToBrain時,它作業正常并回傳串列,當它是另一個值時,它不起作用
這是我的 JSON 檔案的鏈接:https ://drive.google.com/file/d/1JAtELt0VQBCYLrqBY03Ym5BSqZeigE8Z/view?usp=sharing
uj5u.com熱心網友回復:
我相信這個錯誤很可能是因為 JSON 中有拼寫錯誤。在許多應該說的地方
DecCode
它實際上說
DeCode
uj5u.com熱心網友回復:
如果您的JSON結構是正確的,您可以將您的粘貼到下面JSON的鏈接中,單擊轉換并免費獲取您的 Dart 課程。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/517202.html
標籤:json扑镖
