首页 > 解决方案 > 如何将 json 序列化字典恢复为原始对象形式

问题描述

因此,我试图将一个对象保存到一个 json 文件中,然后将该对象恢复为允许我调用其函数的状态。例如,

listofobjects = [<object>, <object>]
json.dump(listofobjects.to_json(), 'file.json') #to_json is a function within the package I'm using to get the object in question to an object
file = open("file.json", "r")

contents = file.load(file)
#dictionary = ast.literal_eval(contents)
d = json.loads(contents)
d.objectfunction()

不过,我无法将 json 字典恢复到对象中。这是我当前状态的代码:

from bitcoinlib.wallets import Wallet #package that I'm using for the "Wallet" object
temp = []
fixedstring = ("id9126923222i09712")
try:
  temp.append(Wallet.create(fixedstring))
  for i in temp:
    with open("backup.json", "wb+") as fp:
      listing = list(fp)
      if temp not in listing:
        dump(temp[0].as_json(), fp, fmt='json')
      else:
        pass
except:
  pass
file = open("backup.json", "r")
contents = json.load(file)
d = json.loads(contents)

file.close()

print(d)

D的输出如下:

{'wallet_id': 1, 'name': 'id9126923222i09712', 'owner': '', 'scheme': 'bip32', 'witness_type': 'legacy', 'main_network': 'bitcoin', 'main_balance': 0.0, 'main_balance_str': '0.00000000 BTC', 'balances': [], 'default_account_id': 0, 'multisig_n_required': 1, 'cosigner_wallet_ids': [], 'cosigner_public_masters': [], 'sort_keys': True, 'main_key_id': 1, 'encoding': 'base58', 'keys': [{'name': 'id9126923222i09712', 'wallet_id': 1, 'key_type': 'bip32', 'account_id': 0, 'balance': 0, 'address': '1M1phKdSmTJsCx6Fvc9zEaDDRETzTrE1dj', 'depth': 0, 'used': False, 'cosigner_id': None, 'change': 0, 'network_name': 'bitcoin', 'encoding': 'base58', 'address_index': 0, 'latest_txid': None, 'purpose': 44, 'public': "b'\\x02bq\\x10\\x88\\xa8\\xa6\\x1d\\x8e\\x01`\\xec\\xe3>\\xc5@?\\xa9\\x1a>\\t\\xd4\\x83\\x9ds\\xbc\\x18\\x07,\\t\\x8b\\x91\\x85'", 'id': 1, 'is_private': True, 'parent_id': 0, 'path': 'm', 'compressed': True}, {'name': 'purpose 44', 'wallet_id': 1, 'key_type': 'bip32', 'account_id': 0, 'balance': 0, 'address': '1MQhXFtW32SySZKi8EzTUNnbzePSkv8GhJ', 'depth': 1, 'used': False, 'cosigner_id': None, 'change': 0, 'network_name': 'bitcoin', 'encoding': 'base58', 'address_index': 2147483692, 'latest_txid': None, 'purpose': 44, 'public': "b'\\x02\\x11\\xa0o\\xe8(\\xb8\\xd3Z\\xb3\\x7fq\\xf1\\xf1\\x04\\x80\\x03\\xdf\\xda\\x86\\xd6S\\xfd[F\\x04zz\\xdd\\xc2\\xa6-\\xcf'", 'id': 2, 'is_private': True, 'parent_id': 1, 'path': "m/44'", 'compressed': True}, {'name': 'coin type 0', 'wallet_id': 1, 'key_type': 'bip32', 'account_id': 0, 'balance': 0, 'address': '1N9daznd2y6k5p2GCg3JdEUKc6n99eEXyb', 'depth': 2, 'used': False, 'cosigner_id': None, 'change': 0, 'network_name': 'bitcoin', 'encoding': 'base58', 'address_index': 2147483648, 'latest_txid': None, 'purpose': 44, 'public': "b'\\x03\\xb2\\xc0\\xd9.v[&\\xf0\\xd19\\x1e\\xf4\\xdb\\xe8+\\x96\\xf1\\x90\\x1bW\\xc0r\\xa5\\x0b\\x01\\x85?\\x19\\xd1\\x00\\x08\\xb0'", 'id': 3, 'is_private': True, 'parent_id': 2, 'path': "m/44'/0'", 'compressed': True}, {'name': 'account 0', 'wallet_id': 1, 'key_type': 'bip32', 'account_id': 0, 'balance': 0, 'address': '1PZSzHsaBgrEktzTyrmaf3k1PuapTX4pSU', 'depth': 3, 'used': False, 'cosigner_id': None, 'change': 0, 'network_name': 'bitcoin', 'encoding': 'base58', 'address_index': 2147483648, 'latest_txid': None, 'purpose': 44, 'public': 'b\'\\x02\\xc6B?4Ie\\x8f\\x11\\xe2\\x8a*"\\x06}\\xcb\\xfc\\xfb\\x89\\xe7\\xea\\xe7k\\x81\\xd0;=\\xd7%\\xdd\\x94ez\'', 'id': 4, 'is_private': True, 'parent_id': 3, 'path': "m/44'/0'/0'", 'compressed': True}, {'name': 'change 0', 'wallet_id': 1, 'key_type': 'bip32', 'account_id': 0, 'balance': 0, 'address': '1EEvVqHJBJTX9BHSGeLa6qinDYVvbRWNTn', 'depth': 4, 'used': False, 'cosigner_id': None, 'change': 0, 'network_name': 'bitcoin', 'encoding': 'base58', 'address_index': 0, 'latest_txid': None, 'purpose': 44, 'public': "b'\\x03E\\x99\\xd4$\\xfa&\\xb1\\xdb\\n\\xe0Y5\\x8e\\x83\\xf9\\x87\\x89)\\x87\\xf73_uE\\x15\\x10\\x99\\xc6X\\xcb\\x17\\xb6'", 'id': 5, 'is_private': True, 'parent_id': 4, 'path': "m/44'/0'/0'/0", 'compressed': True}, {'name': 'address index 0', 'wallet_id': 1, 'key_type': 'bip32', 'account_id': 0, 'balance': 0, 'address': '1Q8vNucidW6HL6gNHk1w4vf19ZduhBnqKE', 'depth': 5, 'used': False, 'cosigner_id': None, 'change': 0, 'network_name': 'bitcoin', 'encoding': 'base58', 'address_index': 0, 'latest_txid': None, 'purpose': 44, 'public': "b'\\x03l\\xe3\\xac$\\xac\\xe1\\x80\\x8e!\\x1a\\x93G\\x84\\xfd\\x81Q\\xf6\\xf7`B\\x1c\\\\M\\xd6\\xea\\xc7d\\x01u\\xf7\\x9e\\xfe'", 'id': 6, 'is_private': True, 'parent_id': 5, 'path': "m/44'/0'/0'/0/0", 'compressed': True}], 'transactions': []}

因此,我基本上需要将生成的对象转换为 json dict(完成,如​​上所示),然后将其恢复为原始对象形式以再次调用它。我该怎么做呢?

标签: pythonobjectpickle

解决方案


推荐阅读