【问题标题】:How to write JSON object into JSON array using Python?如何使用 Python 将 JSON 对象写入 JSON 数组?
【发布时间】:2020-03-30 18:38:31
【问题描述】:

我有一个为机器生成对象的 Python 文件。示例如下。

 {
 "Date/Time": "2019-01-01 8:00:00",
 "Availability": 68,
 "Performance": 70,
 "Quality": 70
 }

我有另一个带有 JSON 数据的 Python 文件,如下所示。如何将 JSON 对象插入到“机器”下的数据数组(第 18 行,空括号)中,而无需将对象复制并粘贴到第二个文件中?是否涉及将 Python 文件写入另一个 Python 文件?

[{
"countries": [{
    "countryID": "79",
    "countryName": "USA",
    "states": [{
        "stateID": "58",
        "stateName": "VA",
        "cities": [{
            "cityID": "13",
            "cityName": "RES",
            "locations": [{
                "locationID": "48",
                "locationName": "RTC",
                "locationZIP": 11111,
                "machines": [{
                    "machineID": "98",
                    "machineName": "RED",
                    "data": []
                    }]
                }]
            }]
        }]
    }]
}]

最终目标:

[{
"countries": [{
"countryID": "79",
"countryName": "USA",
"states": [{
    "stateID": "58",
    "stateName": "VA",
    "cities": [{
        "cityID": "13",
        "cityName": "RES",
        "locations": [{
            "locationID": "48",
            "locationName": "RTC",
            "locationZIP": 11111,
            "machines": [{
                "machineID": "98",
                "machineName": "RED",
                "data": [{
                    "Date/Time": "2019-01-01 8:00:00",
                    "Availability": 68,
                    "Performance": 70,
                    "Quality": 70
                  }]
               }]
            }]
         }]
      }]
   }]
}]

【问题讨论】:

  • 这不就是读取JSON,修改,写回文件吗?

标签: python arrays json file object


【解决方案1】:

您可以执行json.loads 以便能够使用 Python 数据结构,然后映射到目标结构,然后执行 json.dumps 以写回目标 JSON 文件。

import json

with open(file_name) as file:
    string = file.read()
    obj = json.loads(string)

target = map_to_target(obj)

with open(target_file_name, 'w') as out_file:
    out = json.dumps(target)
    out_file.write(out)

【讨论】:

  • 您发布的代码是否应该是第三个单独的文件?还是应该插入到对象生成文件或JSON结构文件中?
  • 我会将它与对象生成器文件分开,以免混淆。它是 python 代码,需要适应您读取日期信息并集成它的用例。为此,map_to_target 需要相应地实施。
  • 仍然对map_to_target 的含义感到困惑。我在映射什么?
  • 当然这一切都取决于你想如何组织它,但一般来说,任务是将一个 JSON 结构 A 转换为第二个 JSON 结构 B。为了更容易我们先翻译将所有 JSON 转换为 Python 对象。我们进行从 Python 对象 A 到 Python 对象 B 的映射 - 即通过方法 map_to_target。完成后,我们将 Python 对象 B 转换为 JSON 结构 B 并完成。
  • 因为我的 JSON 结构中有 6 层(国家、州、城市、位置、机器、数据),所以我有 6 次地图?
【解决方案2】:

您可以编写一个简单的 python 脚本来解码较大的 json 文件,并插入较小的文件(也对其进行解码)。

假设您要添加的数据在一个名为“add_me.txt”的文件中,而大数据文件是“add_to_me.txt”。如您所示,这两个文件都包含 json 对象。你可以这样做:

import json
with open(“add_me.txt”, “r”) as add_me, open(“add_to_me.txt”, “r+) as add_to_me:
    add_me_data = json.loads(add_me.read())
    add_to_me_data = json.loads(add_to_me.read())
    add_to_me.seek(0)

    add_to_me_data[<the right place>] = add_me_data
    add_to_me.write(json.dumps(add_to_me_data))

【讨论】:

  • 我只能用这个模板添加 .txt 文件? .py 呢?
  • 你可以使用任何你想要的格式来访问数据,只要它是一个json字符串......
猜你喜欢
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多