1

Beim Abfragen des nbb-Datenspeichers der App Engine erhalten wir eine Liste wie diese.An die zurückgegebene Google App Engine anfügen ndb-Abfrageliste

[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)] 

dann können wir konvertieren es dict und json kodieren es so etwas zu bekommen:

[ 
    { 
    "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" 
    } 
] 

von query.fetch_page() wir die Verwendung cursor Wert im Gegenzug bekommen. Ich möchte es in der zurückgegebenen Liste, bevor JSON es codiert. Ich kann es einfach durch list.append() Methode anhängen, aber es wäre nicht so wichtig, Wert. Ich brauche es wie:

[ 
    { 
    "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 
] 

Danke.

Hinweis: obige Liste und JSON sind nur eine Darstellung nicht tatsächlich zurückgegebene Daten, so dass die Werte falsch sein können.

+1

Das ist eine seltsame Darstellung Liste sollte gleiche Art von Element enthält. Sollte es nicht wie folgt sein {result: [], cursor = xxx, more = True} – marcadian

Antwort

1

Das Problem ist, dass die Darstellung, die Sie wollen, nicht gültig json ist.

Sie könnten etwas Ähnliches erhalten, indem Sie:

results, cursor, more = query.fetch_page() 
dict_representation = {"results": results, "cursor": cursor, "more": more} 
json_representation = json.dumps(dict_representation) 

Und das Ergebnis wird in etwa wie folgt sein:

{ 
    "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 
}