python将多个json请求合并到一个文件中并保存i

2024-10-02 14:16:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道这个要求有点重。不确定是否有人能帮忙,但如果可以的话,我会很感激的。在

我尝试用python处理XML数据,这对我来说是新的。我试图编写一个脚本来解析几个json请求并将它们组合成一个。我想每隔x个时间将这个文件输入到一个专有系统中,以保持一个位置上的所有数据是最新的。在

            {
                "items": [{
                    "id": 333512,
                    "full_name": "Flooring",
                    "instock": true,
                    "dept": "none",
                    "stockid": 4708384,
                    "commonname": "StdFloor",
                    "reorder": true
                }, {
                    "id": 3336532,
                    "full_name": "Standard Tool",
                    "instock": true,
                    "dept": "none",
                    "stockid": 4708383,
                    "commonname": "StandardTool",
                    "reorder": true
                }]
            }

其中200多个将在初始请求中返回

首先,我需要从每个条目中获取id并运行一个单独的请求来获取每个条目的更多细节。我知道如何运行请求,但如何仅使用id创建一个数组?在

一旦我运行了这些请求,我将得到每项5-6个发票详细信息。例如,这些都属于初始响应中的条目id 333512

^{pr2}$

这些发票中没有项目id,因此当我使用请求URL中的id取回它们时,我想将它们作为项目id的子数组添加到原始项目列表中,就像它们是带着原始请求返回的一样。所以每个项目都会附上发票。我假设最好是按顺序运行所有请求,并创建一个以id作为每个成员名称的数组?在

例如,我希望以这样的方式结束(但是格式正确)。在

            [{
                    "items": [{
                        "id": 333512,
                        "full_name": "Flooring",
                        "instock": true,
                        "dept": "none",
                        "stockid": 4708384,
                        "commonname": "StdFloor",
                        "reorder": true"
                            {
                            "invoices": [{
                                    "invoice_id": 10015,
                                    "cusbillable": true,
                                    "inventoried": false,
                                    "totals": 2.0,
                                    "totalswh": 0.0,
                                    "title": "EarlyOrder",
                                    "invoicerate": 0.0,
                                    "invoicedamt": 0.0,
                                    "stockcost": 0.0,
                                    "remainingbudg": null
                                },
                                {
                                    "invoice_id": 10016,
                                    "title": "EarlyOrder",
                                    "cusbillable": true,
                                    "inventoried": false,
                                    "totals": 2.0,
                                    "totalswh": 0.0,
                                    "invoicerate": 0.0,
                                    "invoicedamt": 0.0,
                                    "stockcost": 0.0,
                                    "remainingbudg": null
                                }],
                            }
                        }],
                        {
                        "id": 3336532,
                        "full_name": "Standard Tool",
                        "instock": true,
                        "dept": "none",
                        "stockid": 4708383,
                        "commonname": "StandardTool",
                        "reorder": true"
                            {
                            "invoices": [{
                                    "invoice_id": 10015,
                                    "cusbillable": true,
                                    "inventoried": false,
                                    "totals": 2.0,
                                    "totalswh": 0.0,
                                    "title": "EarlyOrder",
                                    "invoicerate": 0.0,
                                    "invoicedamt": 0.0,
                                    "stockcost": 0.0,
                                    "remainingbudg": null
                                },
                                {
                                    "invoice_id": 10016,
                                    "title": "EarlyOrder",
                                    "cusbillable": true,
                                    "inventoried": false,
                                    "totals": 2.0,
                                    "totalswh": 0.0,
                                    "invoicerate": 0.0,
                                    "invoicedamt": 0.0,
                                    "stockcost": 0.0,
                                    "remainingbudg": null
                                }],
                            }
            }]

Tags: 项目namenoneidfalsetrueinvoicereorder
1条回答
网友
1楼 · 发布于 2024-10-02 14:16:49

关于如何将项目和发票数据汇编到一个字典中的原则回答:

# The container for compiled items/invoices
items = {}

# Process an items request
for data in jsondata["items"]:
    items[data["id"]] = data
    items[data["id"]]["invoices"] = {}

# Process an invoices request
id = # Your way to get the associated id
items[id]["invoices"] = jsondata["invoices"]

希望对你有帮助。在

相关问题 更多 >

    热门问题