I am trying to follow the steps indicated, as shown in the code:
FUNCTION enviar_factura
LPARAMETER lofactura
SET STEP ON
DO wwJsonSerializer
loSer = CREATEOBJECT("wwJsonSerializer")
lofactura1 = loSer.Serialize(lofactura)
lofactura2 = loSer.DeserializeJson(lofactura1)
CREATE CURSOR lofactura3 (number c(20), prefix c(4), type_document_id c(2))
CollectionToCursor(lofactura2,"lofactura3")
but I get the following error:
"Property LOCOLLECTION is not found."
On which line? And what's the command on that line?
On the highlighted line
What are you serializing? You need to check the values of the things along the way. Use the debugger to check what the JSON is, what the deserialized object is (it's likely null based on the result you're seeing and thereform the JSON is probably null or not what you think it is).
Garbage in, Garbage out.
+++ Rick ---
Next I show the images of the serialized and unserialized object in the debugger. For me everything is fine, I can't find the error, I have already thought about this case a lot.
I'm not sure what you're trying to do, but it's unlikely to work by trying to read the data into a cursor via serialization...
You are starting with an object not a cursor and you're serializing that into JSON string. When you deserialize you end up with the same nested object not a cursor or array/collection. And why would you want to? You already have the data in an object that you can read the data out of. But it's not a list of any sort but a single object so why do you need a cursor?
If for some reason you actually need the data in a table, you can pull the data out of the object in memory and manually assign the values to the columns in the table.
+++ Rick ---
I am calling an api passing it a json file, as I see the function receives an object, I need to convert that object into a cursor, I have not been able to go through the elements of that object, doing it with "for each item in locollection foxobject" the error that I already published, I have tried to read it in other ways but it has not been possible either.
I tried to do it only with "GATHER NAME lofactura", but the difficulty is that lofactura has other objects, so I don't know how many records I should add to the cursor to recover all the information of the object.
I appreciate your help.
That's because you're not working with a collection or array. You are working with an object to begin with - there's nothing that you can automatically convert into a cursor.
loResult = loSer.Deserialize(lcJson) && result is a JSON **OBJECT**
? loResult.Customer.Address
? loResult.date && string
loLineItems = loResult.Invoice_Lines
FOR x = 0 to loLineItems.Count
loItem = loLineItems.Item(x)
? " " + loItem.Description
ENDFOR
You can potentially use CollectionToCursor()
manually to parse the lineitems into a cursor.
Please - take the time to review the documentation - it explains how JSON is mapped to FoxPro and how you can parse.
+++ Rick ---
When using "CollectionToCursor()", I get the error I described above, "Property LOCOLLECTION is not found".
I need to convert that object that I am receiving into a cursor, since the methods that exist do not allow me to read that object, I am going to make my own method that reads the json and converts it into a cursor, unless I have another idea that can help me .
Thank you very much for all your support.