1

对于包含这两个键值对的字典:

str = StringProperty
time = DateTimeProperty

我想将其序列化为 JSON,将其存储在 Datastore 中,然后获取它,并将其反序列化为原始属性。

4

2 回答 2

2

尝试这样做:

d = {
  'str'  : StringProperty,
  'time' : unicode(DateTimeProperty)
}
s = simplejson.dumps(d)
print s
于 2011-01-17T18:03:35.190 回答
0

如果您的值是字符串,这很简单:

>>> import simplejson
>>> print simplejson.dumps({'str': 'StringProperty', 'time': 'DateTimeProperty'})
{"str": "StringProperty", "time": "DateTimeProperty"}

但是,如果这些值是来自自定义类(例如 Google App Engine 属性类)的对象,则它们不是 JSON 可序列化的。

JSON 仅序列化简单的数据类型,例如整数/浮点数、布尔值、字符串、列表/元组和字典。(有关详细信息,请参阅http://www.json.org/。)

为了获得 JSON 可序列化的值,您需要定义一种将它们转换为简单数据类型的方法。例如,将对象转换为包含类名和重建它们所需的参数的元组。

于 2011-01-17T18:09:23.787 回答