Web Connection User Discussions
Weird vfp collection issue
Gravatar is a globally recognized avatar based on your email address. Weird vfp collection issue
  Michael B
  All
  Feb 3, 2018 @ 08:21pm

So I am using a typical DeserializeJson and the object which has a collection will not let me access it as it usually does. The json string is fine, but for whatever reason I cannot access the collection below the root.

loSerializer = Createobject("wwJsonSerializer")
loResponse = loSerializer.DeserializeJson(_cResult)

Checkout the debug of the object - you can see referencing it with loResponse.objects[1] just returns another collection...

Gravatar is a globally recognized avatar based on your email address. re: Weird vfp collection issue
  Michael B
  Michael B
  Feb 4, 2018 @ 08:18am

I solved my own issue. If you look carefully at the object you'll see the contents returned have the word 'objects' in it. This causes a problem for foxpro. The solution was to strtran() the json response and change the name from objects to a non-protected word. I chose to use the word 'data' and problem solved.

Rick - I wonder if there is a trick that could be written into the json parser to not allow protected names from being used in arrays or collections.

Gravatar is a globally recognized avatar based on your email address. re: Weird vfp collection issue
  Rick Strahl
  Michael B
  Feb 5, 2018 @ 01:17pm

Objects is not a reserved word in VFP. I think it's a problem for navigation, but it should be valid. The fact that it exists in your debugger view means it's valid.

Reseved Words can be used as property names. I think there are a couple of things you can't use (Class, Classname, BaseClass if I remember right) but everything else works.

The issue you're running into most likely is that FoxPro can't handle array based and method based . traversal on child collections. You need to store to a variable, then continue the traversal.

The following demonstrates:

loObj = CREATEOBJECT("EMPTY")

ADDPROPERTY(loObj,"Name","Rick")
ADDPROPERTY(loObj,"Objects",CREATEOBJECT("Collection"))
ADDPROPERTY(loObj,"Count",2)

obj2 = CREATEOBJECT("empty")
ADDPROPERTY(obj2,"Company","West Wind")
ADDPROPERTY(obj2,"Address","Paia")
loObj.Objects.Add(obj2)

obj2 = CREATEOBJECT("empty")
ADDPROPERTY(obj2,"Company","EPS")
ADDPROPERTY(obj2,"Address","Wailea")
loObj.Objects.Add(obj2)

? loObj.Name
? loObj.Count
? loObj.Objects[1]
? loObj.Objects.Item(1)

? loObj.Objects.Item(1).Company

*** THIS FAILS! - FoxPro array/collection travesal bug
? loObj.Objects[1].Company

+++ Rick ---

© 1996-2024