简体   繁体   English

从App Engine数据存储到JSON的分层数据输出?

[英]Hierarchical data output from App Engine Datastore to JSON?

I have a large hierarchical dataset in the App Engine Datastore. 我在App Engine数据存储区中有一个大型的分层数据集。 The hierarchy is preserved by storing the data in Entity groups, so that I can pull a whole tree by simply knowing the top element key like so: 通过将数据存储在Entity组中来保留层次结构,这样我就可以通过简单地知道top元素键来提取整棵树,如下所示:

query = db.Query().ancestor(db.get(key))

The question: How do I now output this data as JSON and preserve the hierarchy? 问题:现在如何将这些数据作为JSON输出并保留层次结构?

Google has a utility class called GqlEncoder that add support for datastore query results to simplejson, but it basically flattens the data, destroying the hierarchy. Google有一个名为GqlEncoder的实用程序类,该实用程序类将对数据存储区查询结果的支持添加到simplejson中,但它基本上使数据变平,破坏了层次结构。

Any suggestions? 有什么建议么?

I imagine you're referring to this code and the "flattening" you mention is done by lines 51-52: 我想您正在引用此代码 ,而您提到的“展平”是通过第51-52行完成的:

   if isinstance(obj, db.GqlQuery):
      return list(obj)

while the rest of the code is fine for your purpose. 而其余的代码则适合您的目的。 So, how would you like to represent a GQL query, since you don't what a JS array (Python list) of the objects it contains? 那么,由于您不包含它的对象的JS数组(Python列表),您想如何表示GQL查询? It's not clear, besides the entity group (which you're recovering entirely), what gives it hierarchy; 除了实体组(您要完全恢复它)之外,尚不清楚什么赋予它层次结构; is it an issue of "parent"? 是“父母”问题吗?

Anyway, once that's clarified, copying and editing that file into your own code seems best (it's not designed to let you override just that one tidbit). 无论如何,一旦弄清楚了,将文件复制并编辑到您自己的代码中似乎是最好的(它的目的不是让您仅覆盖那个花絮)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM