Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python 3从嵌套JSON中提取数据_Json_Python 3.x_Nested Loops - Fatal编程技术网

使用python 3从嵌套JSON中提取数据

使用python 3从嵌套JSON中提取数据,json,python-3.x,nested-loops,Json,Python 3.x,Nested Loops,正在寻找从嵌套json中提取数据的方法。 数据以xml形式从windows调度器中提取,并转换为json。 它代表操作系统计划的任务,大约有100个任务,我的目标是将信息从json提取到外部数据库。 这就是我的json文件的样子: { "Tasks": { "Task": [ { "RegistrationInfo": { "Author": "Adobe Systems In

正在寻找从嵌套json中提取数据的方法。 数据以xml形式从windows调度器中提取,并转换为json。 它代表操作系统计划的任务,大约有100个任务,我的目标是将信息从json提取到外部数据库。 这就是我的json文件的样子:

{
    "Tasks": {
        "Task": [
            {
                "RegistrationInfo": {
                    "Author": "Adobe Systems Incorporated",
                    "Description": "This task keeps your Adobe Reader and Acrobat applications up to date with the latest enhancements and security fixes",
                    "URI": "\\Adobe Acrobat Update Task"
                },
                "Principals": {
                    "Principal": {
                        "GroupId": "S-1-5-4",
                        "_id": "Author"
                    }
                },
                "Settings": {
                    "DisallowStartIfOnBatteries": "true",
                    "StopIfGoingOnBatteries": "true",
                    "MultipleInstancesPolicy": "IgnoreNew",
                    "StartWhenAvailable": "true",
                    "IdleSettings": {
                        "Duration": "PT10M",
                        "WaitTimeout": "PT1H",
                        "StopOnIdleEnd": "true",
                        "RestartOnIdle": "false"
                    }
                },
                "Triggers": {
                    "LogonTrigger": {
                        "StartBoundary": "2013-08-01T12:05:00",
                        "EndBoundary": "2027-05-02T08:00:00",
                        "Delay": "PT12M",
                        "Repetition": {
                            "Interval": "PT3H30M"
                        },
                        "_id": "TriggerUserLoggon"
                    },
                    "CalendarTrigger": {
                        "StartBoundary": "2013-01-01T09:00:00",
                        "EndBoundary": "2027-05-02T12:05:00",
                        "ScheduleByDay": {
                            "DaysInterval": "1"
                        },
                        "_id": "TriggerDaily"
                    }
                },
                "Actions": {
                    "Exec": {
                        "Command": "C:\\Program Files (x86)\\Common Files\\Adobe\\ARM\\1.0\\AdobeARM.exe"
                    },
                    "_Context": "Author"
                },
                "_xmlns": "http://schemas.microsoft.com/windows/2004/02/mit/task",
                "_version": "1.2"
            },
            {
                "RegistrationInfo": {
                    "Author": "Adobe",
                    "Description": "This task keeps your Adobe Flash NPAPI Player installation up to date with the latest enhancements and security fixes. If this task is disabled or removed, Adobe Flash Player will be unable to automatically secure your machine with the latest security fixes.",
                    "URI": "\\Adobe Flash Player NPAPI Notifier"
                },
                "Principals": {
                    "Principal": {
                        "UserId": "S-1-5-21-2755204513-1269241785-1912306605-1001",
                        "LogonType": "InteractiveToken",
                        "_id": "Author"
                    }
                },
                "Settings": {
                    "DisallowStartIfOnBatteries": "false",
                    "StopIfGoingOnBatteries": "true",
                    "MultipleInstancesPolicy": "IgnoreNew",
                    "StartWhenAvailable": "true",
                    "RunOnlyIfNetworkAvailable": "true",
                    "IdleSettings": {
                        "Duration": "PT10M",
                        "WaitTimeout": "PT1H",
                        "StopOnIdleEnd": "true",
                        "RestartOnIdle": "false"
                    }
                },
                "Triggers": {
                    "CalendarTrigger": {
                        "StartBoundary": "1999-12-31T16:14:00-08:00",
                        "Repetition": {
                            "Interval": "PT1H",
                            "Duration": "P1D"
                        },
                        "ScheduleByDay": {
                            "DaysInterval": "7"
                        },
                        "_id": "NotificationTrigger"
                    }
                },
                "Actions": {
                    "Exec": {
                        "Command": "C:\\Windows\\SysWOW64\\Macromed\\Flash\\FlashUtil32_32_0_0_330_Plugin.exe",
                        "Arguments": "-check plugin"
                    },
                    "_Context": "Author"
                },
                "_xmlns": "http://schemas.microsoft.com/windows/2004/02/mit/task",
                "_version": "1.2"
            }
下面的代码只适用于一个任务,但我需要从每个任务中获取数据,我想我需要找到一种方法,将任务数插入到index的值中,并逐个循环,但我尝试的方法都不起作用

import json
index=1

with open('name.json', 'r') as f:
    array = json.load(f)

ts=(array['Tasks'])
print('Author is',ts['Task'][index]['RegistrationInfo']['Author'])
print('Description is' ,ts['Task'][index]['RegistrationInfo']['Description'])
print('URI is',ts['Task'][index]['RegistrationInfo']['URI'])
print('user ID is', ts['Task'][index]['Principals']['Principal']['UserId'])
print('Logon Type  is', ts['Task'][index]['Principals']['Principal']['LogonType'])
print(' ID is', ts['Task'][index]['Principals']['Principal']['_id'])
print(' DisallowStartIfOnBatteries is ', ts['Task'][index]['Settings']['DisallowStartIfOnBatteries'])
print(' StopIfGoingOnBatteries is ', ts['Task'][index]['Settings']['StopIfGoingOnBatteries'])
print(' MultipleInstancesPolicy is ', ts['Task'][index]['Settings']['MultipleInstancesPolicy'])
print(' StartWhenAvailable is ', ts['Task'][index]['Settings']['StartWhenAvailable'])
print(' RunOnlyIfNetworkAvailable is ', ts['Task'][index]['Settings']['RunOnlyIfNetworkAvailable'])

你以前用过循环吗?常规循环?当然。那就迭代任务?这是我有问题的部分。当尝试迭代时,它不起作用。甚至无法计算文件中的任务数。我很乐意提供帮助,但我仍然不确定您需要打印什么。代码中显示的那些任务具有各种属性。您如何知道要从每个任务中提取哪些描述和属性?以前是否使用过循环?常规循环?当然。那就迭代任务?这是我有问题的部分。当尝试迭代时,它不起作用。甚至无法计算文件中的任务数。我很乐意提供帮助,但我仍然不确定您需要打印什么。代码中显示的那些任务具有各种属性。您如何知道要从每个任务中提取哪些描述和属性?