I have this json file :
{
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: ""
}
]
}
Here I try to display specific data from the json (here I want to display what's in "projects")
var resp = response.body;
print("mydata : " resp['projects'].toString());
Then I tried adding a jsonDecode :
var resp = jsonDecode(response.body);
print("mydata : " resp['projects'].toString());
Then jsonEncode :
var resp = jsonEncode(response.body);
print("mydata : " resp['projects'].toString());
Then both :
var resp = jsonDecode(jsonEncode(response.body));
print("mydata : " resp['projects'].toString());
When I try display only "resp" it works, but it's the access to a specific part (like "projects") of the JSON that doesn't work. The only thing that work is with an int and it display the specific caractere of the index as if the JSON was read as a String.
var resp = jsonDecode(jsonEncode(response.body));
print("mydata : " resp[0].toString());
//display the first caracter of the JSON
The thing is when I read the response of POST HTTP requests, using jsonEncode then jsonDecode works perfectly, I can then easily access any part of the 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
But all this doesn't work when trying to access specific parts of my first JSON file.
Is there something wrong with my JSON file ?
Thanks for helping.
Update : commas are missing in the json, I corrected it but still the same probleme
CodePudding user response:
I ran your JSON through a JSON formatter / validator and yes, your json was missing some commas and whatnot.
Check out this Gist I created for your decoding - run it through DartPad.Dev and check out.
I made sure the JSON was a proper string before I ran it through the jsonDecode, then I just simply access its values like this:
var decodedJSON = jsonDecode(json);
print(decodedJSON['projects'].length);
print(decodedJSON['projects'][0]['projectName']);
print(decodedJSON['avatars'].length);
print(decodedJSON['avatars'][0]['resourceName']);
I got the values I expected:
3
Complexe hotelier croisette
5
Alain Martoni
I think your issue was just badly formatted JSON coming out of the pipe. Check out the Gist and let me know.