擅长:python、mysql、java
<p>这个问题的另一个可能的解决方案是<a href="http://jsonpickle.github.io/" rel="nofollow noreferrer">jsonpickle</a>,它可以用来将任何Python对象转换为JSON(不仅仅是简单的列表)。</p>
<p>从<strong>jsonpickle</strong>主页:</p>
<blockquote>
<p>jsonpickle is a Python library for serialization and deserialization
of complex Python objects to and from JSON. The standard Python
libraries for encoding Python into JSON, such as the stdlib’s json,
simplejson, and demjson, can only handle Python primitives that have a
direct JSON equivalent (e.g. dicts, lists, strings, ints, etc.).
jsonpickle builds on top of these libraries and allows more complex
data structures to be serialized to JSON. jsonpickle is highly
configurable and extendable–allowing the user to choose the JSON
backend and add additional backends.</p>
</blockquote>
<p>执行转换很简单:</p>
<pre><code>import jsonpickle
class JsonTransformer(object):
def transform(self, myObject):
return jsonpickle.encode(myObject, unpicklable=False)
</code></pre>