简体   繁体   English

“AttributeError: 'list' object 没有属性 'items'” 导入 Json 时

[英]"AttributeError: 'list' object has no attribute 'items'" While importing Json

I get the following error when i try to use the "deleted" part of my json file to change one field (ReportedStatusField) for each "Facility" in my Database that matches with the id's inside "deleted":当我尝试使用我的 json 文件的“已删除”部分为我的数据库中与“已删除”内的 ID 匹配的每个“设施”更改一个字段 (ReportedStatusField) 时,出现以下错误:

  File "C:\users\management\commands\deactivate_facilities.py", line 42, in handle
    for key, data in data_object.items():
AttributeError: 'list' object has no attribute 'items'

It's basically a list of UUID's of Facilities that closed so i change the status this way with a json file that gets imported with a url.它基本上是关闭设施的 UUID 列表,因此我使用 json 文件以这种方式更改状态,该文件通过 url 导入。

import requests
import json

from users.models import Facility
from django.core.management.base import BaseCommand

IMPORT_URL = 'https://domain/file.json'

class Command(BaseCommand):
    def import_facility_from_file(self, data):
                    key = data.get('key', None)


                    if Facility.objects.filter(key=UUID):
                        
                        msg = "\n\nFacility closed: {}\n{}".format(key, str())
                        print(msg)
                        facility, facility_closed = Facility.objects.update_or_create(key=UUID,
                            defaults={
                            'ReportedStatusField': 'closed'
                            }
                        )                        

    def handle(self, *args, **options):

        headers = {'Content-Type': 'application/json'}
        response = requests.get(
            url=IMPORT_URL,
            headers=headers,
        )

        response.raise_for_status()
        data = response.json()

        for key, data_object in data.items():
            if key in ["deleted"]:
                for key, data in data_object.items():
                    self.import_facility_from_file(data)

My JSON我的 JSON

{
"added":
{"125hk24h5kjh43k5":
{
"UUID":"125hk24h5kjh43k5",
"Name":"Test Facility 1",
"AddressInfo":
{"PrimaryAddress":"1234 Drive RD"},
"ImporterLastModifiedTimestamp":1643721420}},


// This is the "list" of deleted Facilities

"deleted":["235hk24h5kjh43k5,235hk345789h43k5"],

"modified":{"995hk24h5kjh43k5":
{
"UUID":"995hk24h5kjh43k5",
"Name":"Test Facility 2",
"AddressInfo":
{"PrimaryAddress":"2345 Test RD"},
"ImporterLastModifiedTimestamp":1643721420}
}
}

Whenever we use 'items' method on the dictionary, it will return the tuple type element in the list.每当我们在字典上使用'items'方法时,它都会返回列表中的元组类型元素。 and we cannot run 'item' method on the list我们不能在列表上运行“item”方法

   for key, data_object in data.items():
            if key in ["deleted"]:
                for  data in data_object:
                    self.import_facility_from_file(data)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 'list' object 没有属性 'items' - Python - AttributeError: 'list' object has no attribute 'items' - Python AttributeError:'list'对象在scrapy中没有属性'items' - AttributeError: 'list' object has no attribute 'items' in a scrapy AttributeError:导入SAS数据集时,“ bool”对象没有属性“ sum” - AttributeError: 'bool' object has no attribute 'sum' while importing SAS dataset AttributeError: 'int' object 在使用 json 和字典时没有属性 'items' - AttributeError: 'int' object has no attribute 'items' when working with json and dictionaries cx 冻结:AttributeError:'list' 对象没有属性 'items' - cx freeze : AttributeError: 'list' object has no attribute 'items' AttributeError:“列表”对象没有属性“ _Stack__items” - AttributeError: 'list' object has no attribute '_Stack__items' Python WordCloud遇到AttributeError:'list'对象没有属性'items' - Python WordCloud meet AttributeError: 'list' object has no attribute 'items' 获取AttributeError:将.log文件格式转换为.json格式时,“列表”对象没有属性“拆分” - Getting AttributeError: 'list' object has no attribute 'split' while converting the .log file formate to .json format AttributeError:在Python中解析JSON时,“模块”对象没有属性“加载” - AttributeError: 'module' object has no attribute 'loads' while parsing json in python AttributeError: 'list' object 没有属性 - AttributeError: 'list' object has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM