我有這個 json 檔案:
{
projects: [
{
projectId: 3
projectName: "Complexe hotelier croisette",
workflow: "en cours",
realCalendars: [
{
id: 29,
start: 1630590443000
end: 1648475243000
type: 0
remark: NULL
status: 0
calendarId: NULL
}
],
scheduledCalendars: [
{
id: 30,
start: 1630590443000
end: 1646059643000
type: 0
remark: NULL
status: 0
calendarId: NULL
}
],
lastPlanningModif: 1639320443000
planned: 164,
actual: 142,
remaining: 25,
resourceTaskNumber: 1,
projectTaskNumber: 18,
leaderId: 8,
resourceIds: [8, 7, 6],
dashbordField: 3,
dashbordTendency: 1,
weatherComments: [
{
message: "weather 5",
date: 1645195643000
source: 1
taskId:0
projectId: 3
authorId: 8,
messageId: 31,
messageParentId: 32
},
{
message: "weather 6",
date: 1643813243000
source: 1
taskId: 0
projectId: 3
authorId: 8,
messageId: 33,
messageParentId: 34
},
{
message: "weather 7",
date: 1642776443000
source: 1
taskId: 0
projectId: 3
authorId: 7,
messageId: 35,
messageParentId: 36
},
],
projectComments: [
{
message: "Time 3",
date: 1642776443000
source: 4
taskId: 3789
projectId: 3
authorId: 7,
messageId: 37,
messageParentId: 38
},
{
message: "Time 4",
date: 1639838843000
source: 4
taskId: 3876
projectId: 3
authorId: 8,
messageId: 39,
messageParentId: 40
},
{
message: "Time 5",
date: 1638888443000
source: 2
taskId: 3258
projectId: 3
authorId: 8,
messageId: 41,
messageParentId: 42
},
],
},
{
projectId: 2
projectName: "Festival de la moutarde de Dijon",
workflow: "objectif",
realCalendars: [
{
id: 23,
start: 1682689643000
end: 1696686443000
type: 0
remark: NULL
status: 0
calendarId: NULL
}
],
scheduledCalendars: [
{
id: 24,
start: 1682689643000
end: 1696686443000
type: 0
remark: NULL
status: 0
calendarId: NULL
}
],
lastPlanningModif: 1646059643000
planned: 150,
actual: 0,
remaining: 150,
resourceTaskNumber: 2,
projectTaskNumber: 10,
leaderId: 4,
resourceIds: [4, 6, 7, 8],
dashbordField: 4,
dashbordTendency: 2,
weatherComments: [
{
message: "weather 4",
date: 1674744443000
source: 1
taskId: 0
projectId: 1
authorId: 4,
messageId: 25,
messageParentId: 26
}
],
projectComments: [
],
},
{
projectId: 1
projectName: "Red Is Dead",
workflow: "en cours",
realCalendars: [
{
id: 9,
start: 1642430843000
end: 1669387643000
type: 0
remark: NULL
status: 0
calendarId: NULL
}
],
scheduledCalendars: [
{
id: 10,
start: 1642430843000
end: 1666705643000
type: 0
remark: NULL
status: 0
calendarId: NULL
}
],
lastPlanningModif: 1645109243000
planned: 280,
actual: 58,
remaining: 222,
resourceTaskNumber: 5,
projectTaskNumber: 36,
leaderId: 4,
resourceIds: [4,5,6],
dashbordField: 2,
dashbordTendency: 3,
weatherComments: [
{
message: "weather 1",
date: 1645800443000
source: 1
taskId: 0
projectId: 1
authorId: 4,
messageId: 11,
messageParentId: 12
},
{
message: "weather 2",
date: 1643986043000
source: 1
taskId: 0
projectId: 1
authorId: 4,
messageId: 13,
messageParentId: 14
},
{
message: "weather 3",
date: 1643122043000
source: 1
taskId: 0
projectId: 1
authorId: 4,
messageId: 15,
messageParentId: 16
},
],
projectComments: [
{
message: "Time 1",
date: 1642862843000
source: 4
taskId: 1234
projectId: 1
authorId: 5,
messageId: 17,
messageParentId: 18
},
{
message: "Time 2",
date: 1642258043000
source: 4
taskId: 1235
projectId: 1
authorId: 5,
messageId: 19,
messageParentId: 20
},
{
message: "Task 1",
date: 1641826043000
source: 2
taskId: 1234
projectId: 1
authorId: 5,
messageId: 21,
messageParentId: 22
},
],
}
],
avatars: [
{
resourceId: 8,
idSource: 81,
resourceName: "Alain Martoni",
trigram: "AMO",
imgBase64: ""
},
{
resourceId: 4,
idSource: 41,
resourceName: "Odile Deray",
trigram: "ODE",
imgBase64: ""
},
{
resourceId: 7,
idSource: 71,
resourceName: "Patrick Bialès",
trigram: "PBI",
imgBase64: " "
},
{
resourceId: 5,
idSource: 51,
resourceName: "Serge Karamazov",
trigram: "SKA",
imgBase64: ""
},
{
resourceId: 6,
idSource: 61,
resourceName: "Simon Jérémi",
trigram: "SJE",
imgBase64: ""
}
]
}
在這里我嘗試顯示來自 json 的特定資料(這里我想顯示“專案”中的內容)
var resp = response.body;
print("mydata : " resp['projects'].toString());
然后我嘗試添加一個 jsonDecode :
var resp = jsonDecode(response.body);
print("mydata : " resp['projects'].toString());
然后 jsonEncode :
var resp = jsonEncode(response.body);
print("mydata : " resp['projects'].toString());
然后兩者:
var resp = jsonDecode(jsonEncode(response.body));
print("mydata : " resp['projects'].toString());
當我嘗試僅顯示“resp”時,它可以作業,但是對 JSON 的特定部分(如“專案”)的訪問不起作用。唯一有效的是使用 int,它顯示索引的特定字符,就好像 JSON 被讀取為字串一樣。
var resp = jsonDecode(jsonEncode(response.body));
print("mydata : " resp[0].toString());
//display the first caracter of the JSON
問題是當我閱讀 POST HTTP 請求的回應時,使用 jsonEncode 然后 jsonDecode 完美運行,然后我可以輕松訪問 JSON 的任何部分:
var response = await http.post(
Uri.parse(url),
body: jsonEncode({
"jsonrpc": "2.0",
"id": "10",
"method": "method",
"params": [someParams]
})
);
var resp = jsonDecode(response.body);
print("response body : " resp['projects'].toString());
//display the "project" part of the JSON response
但是當嘗試訪問我的第一個 JSON 檔案的特定部分時,所有這些都不起作用。
我的 JSON 檔案有問題嗎?
感謝您的幫助。
更新:json中缺少逗號,我更正了它,但仍然是同樣的問題
uj5u.com熱心網友回復:
我通過 JSON 格式化程式/驗證器運行您的 JSON,是的,您的 json 缺少一些逗號和諸如此類的東西。
查看我為您的解碼創建的這個Gist - 通過DartPad.Dev運行它并查看。
在我通過jsonDecode運行它之前,我確保 JSON 是一個正確的字串,然后我只是簡單地訪問它的值,如下所示:
var decodedJSON = jsonDecode(json);
print(decodedJSON['projects'].length);
print(decodedJSON['projects'][0]['projectName']);
print(decodedJSON['avatars'].length);
print(decodedJSON['avatars'][0]['resourceName']);
我得到了我期望的值:
3
Complexe hotelier croisette
5
Alain Martoni
我認為您的問題只是從管道中流出的格式錯誤的 JSON。查看要點并告訴我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/435549.html
