简体   繁体   English

将 JSON 数组转换为 JSON Object (Python)

[英]Convert JSON Array to JSON Object (Python)

I have an empty JSON Array...我有一个空的 JSON 阵列...

FINAL_ELOGII_DATA_ARRAY = []

That gets populated via.append inside a for loop from a Python Object...这通过 append 填充到来自 Python Object 的 for 循环中...

for ord in FINAL_SHOPIFY_DATA:
    FINAL_ELOGII_DATA_ARRAY.append({
        "date": int(ELOGII_TODAY),
        "customer": str(ord.id),
        "tasks": [
            {
                "date": int(0),
                "customer": str(ord.id),
                "type": int(0),
                "location": {
                    "address": str(ord.shipping_address.address1),
                    "postCode": str(ord.shipping_address.zip),
                    "city": str(ord.shipping_address.city),
                    "country": str(ord.shipping_address.country),
                    "contactName": str(ord.customer.first_name) + ' ' + str(ord.customer.last_name),
                    "contactPhone": str(ord.shipping_address.phone),
                    "contactEmail": str(ord.email),
                    "ref": str(ord.customer.first_name) + ' ' + str(ord.customer.last_name)
                }
            }
        ]
    })

And outputs the following (example):并输出以下内容(示例):

[
   {
      "date":####,
      "customer":"######",
      "tasks":[
         {
            "date":0,
            "customer":"######",
            "type":0,
            "location":{
               "address":"######",
               "postCode":"########",
               "city":"#####",
               "country":"#########",
               "contactName":"#########",
               "contactPhone":"########",
               "contactEmail":"",
               "ref":"#######"
            }
         }
      ]
   }
]

So the JSON array gets populated correctly, however, I need this to be a JSON object rather than an array as the API that I'm posting to doesn't support arrays, only objects. So the JSON array gets populated correctly, however, I need this to be a JSON object rather than an array as the API that I'm posting to doesn't support arrays, only objects. Any tips on converting this to an object?将其转换为 object 的任何提示?

class Container():
    def __init__(self, listData):
        self.listData = listData

for..
 listToSend.append(data) 

container = Container(listToSend)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM