When querying the App engine ndb datastore we get a list like this.
在查询App引擎ndb数据存储区时,我们得到一个这样的列表。
[Example(key=Key('Example', 5136918324969472), content=u'hellow', created_on=datetime.datetime(2016, 5, 21, 13, 6, 25, 784956), date=datetime.datetime(2016, 3, 20, 0, 0), modified_on=None, published=True, soft_deleted=False, stars=0), Example(key=Key('Example', 5699868278390784), content=u'hi how r u!', created_on=datetime.datetime(2016, 5, 21, 13, 6, 25, 568392), date=datetime.datetime(2016, 3, 20, 0, 0), modified_on=None, published=True, soft_deleted=False, stars=0)]
then we can convert it to dict and json encode it to get something like this:
然后我们可以将它转换为dict和json编码它得到这样的东西:
[
{
"modifiedOn": null,
"id": "6051711999279104",
"stars": 0,
"tags": [],
"softDeleted": false,
"date": "2016-03-20 00:00:00",
"content": "hello",
"createdOn": "2016-05-21 13:06:24"
},
{
"modifiedOn": null,
"id": "4925812092436480",
"stars": 0,
"tags": [],
"softDeleted": false,
"date": "2016-03-20 00:00:00",
"createdOn": "2016-05-21 13:06:16"
}
]
By using query.fetch_page()
we can get cursor
value in return. I want it in returned list before json encoding it. i can just append it by list.append()
method but it would not be as key, value. I need it like:
通过使用query.fetch_page(),我们可以获得游标值。我希望它在json编码之前返回列表中。我可以通过list.append()方法追加它,但它不会是关键值。我需要它像:
[
{
"modifiedOn": null,
"id": "6051711999279104",
"stars": 0,
"tags": [],
"softDeleted": false,
"date": "2016-03-20 00:00:00",
"content": "hello",
"createdOn": "2016-05-21 13:06:24"
},
{
"modifiedOn": null,
"id": "4925812092436480",
"stars": 0,
"tags": [],
"softDeleted": false,
"date": "2016-03-20 00:00:00",
"createdOn": "2016-05-21 13:06:16"
},
"cursor": "dhiugdgdwidfwiflfsduifewrr3rdufif",
"more": false
]
Thanks.
Note: above list and json are just a representation not actual returned data so values may be wrong.
注意:上面的列表和json只是一个表示而不是实际返回的数据,所以值可能是错误的。
1 个解决方案
#1
1
The problem is that the representation you want is not valid json.
问题是你想要的表示是无效的json。
You could get something similar by doing:
你可以通过这样做得到类似的东西:
results, cursor, more = query.fetch_page()
dict_representation = {"results": results, "cursor": cursor, "more": more}
json_representation = json.dumps(dict_representation)
And the result will be something like the following:
结果将如下所示:
{
"results":[
{
"modifiedOn":null,
"id":"6051711999279104",
"stars":0,
"tags":[
],
"softDeleted":false,
"date":"2016-03-20 00:00:00",
"content":"hello",
"createdOn":"2016-05-21 13:06:24"
},
{
"modifiedOn":null,
"id":"4925812092436480",
"stars":0,
"tags":[
],
"softDeleted":false,
"date":"2016-03-20 00:00:00",
"createdOn":"2016-05-21 13:06:16"
}
],
"cursor":"dhiugdgdwidfwiflfsduifewrr3rdufif",
"more":false
}
#1
1
The problem is that the representation you want is not valid json.
问题是你想要的表示是无效的json。
You could get something similar by doing:
你可以通过这样做得到类似的东西:
results, cursor, more = query.fetch_page()
dict_representation = {"results": results, "cursor": cursor, "more": more}
json_representation = json.dumps(dict_representation)
And the result will be something like the following:
结果将如下所示:
{
"results":[
{
"modifiedOn":null,
"id":"6051711999279104",
"stars":0,
"tags":[
],
"softDeleted":false,
"date":"2016-03-20 00:00:00",
"content":"hello",
"createdOn":"2016-05-21 13:06:24"
},
{
"modifiedOn":null,
"id":"4925812092436480",
"stars":0,
"tags":[
],
"softDeleted":false,
"date":"2016-03-20 00:00:00",
"createdOn":"2016-05-21 13:06:16"
}
],
"cursor":"dhiugdgdwidfwiflfsduifewrr3rdufif",
"more":false
}