Skip to main content
100%

[json和python对象相互转换] #python

✓ Published0🌍 Public
SShawn91
Last edited Feb 19, 2019
Created on Feb 19, 2019

This example demonstrates two-way conversion between JSON data and Python objects using the standard `json` module. It first shows how `json.loads` with an `object_hook` parameter transforms JSON input into a custom `JSONObject` class, allowing attribute-style access to fields like `data.name` instead of dictionary indexing. The second part illustrates the reverse process with a `serialize_instance` function that converts a `Point` class instance into a dictionary, adding a `__classname__` key to preserve type information. Both conversions rely solely on Python's built-in `json` library and basic class definitions, with no external dependencies.

AI-generated description

Similar vizzes