Deserializes a JSON object array result into a FoxPro cursor or table.
JSON has no concept of 'cursors' and holds columnar data as Arrays. You can serialize cursors and they are serialized into JSON as object arrays. But when that same data is deserialized back into FoxPro it's returned as an Array of objects not a cursor.
To work around this problem this helper method combines
Deserialize() and CollectionToCursor() which requires some extra work by providing a cursor to parse data into.
Objects are created as new instances of EMPTY objects and passed back. Arrays are passed back as FoxPro Collection objects.
DO wwJsonSerializer loSer = CREATEOBJECT("wwJsonSerializer") SELECT TOP 5 * FROM Customers ORDER BY Company INTO CURSOR TQuery CLEAR lcJson = loSer.Serialize("cursor:TQuery", .T.) ? lcJson USE IN TQuery && close *** Create an empty writable cursor as 'schema' to import to SELECT * FROM Customers WHERE 1=0 INTO CURSOR TCustomers READWRITE *** returns 5 and TCustomers now has 5 added customers ? loSer.DeserializeCursor(lcJson, "TCustomers") BROWSE NOWAIT
This method only works with a top level object array and does not work with child properties. The assumption is that the entire result is the table data contained in a single array.
For de-serializing child cursors in JSON, deserialize the entire object, then explicitly use
CollectionToCursor()on the object properties that you want to de-serialize into a cursor.
Number of records imported or -1 on error
A JSON string of an object array to be parsed.
A writable cursor or table alias, which serves as the structure scheme to import the data to. Any fields matched in this cursor are imported from the json data.
See also:Class wwJsonSerialize | wwUtils::CollectionToCursor
Comment or report problem with topic