无法访问 jsonfile 中的特定数据 - Flutter

Can't access specific data in jsonfile - Flutter

我有这个 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());

然后我尝试添加一个json解码:

var resp = jsonDecode(response.body);
print("mydata : " + resp['projects'].toString());

然后json编码:

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 中缺少逗号,我更正了它,但仍然是同样的问题

我 运行 你的 JSON 通过 JSON 格式化程序/验证器,是的,你的 json 缺少一些逗号和诸如此类的东西。

查看此 Gist I created for your decoding - run it through 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 从管道中出来。查看 Gist 并告诉我。