PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → JSON..Member property
JSON..Member property
Iniciado por guest, 21,ago. 2016 13:31 - 4 respuestas
Publicado el 21,agosto 2016 - 13:31
Anyone using the ..Member to retrieve JSON values?
My attempts have bee unsuccessful - any advice, suggestion appreciated.

As a simplified example say I receive a JSON string containing 5 items [ { "ID":"65" }, { "ID":"67" }, { "ID":"71" }, { "ID":"74" }, { "ID":"76" } ]According to the Help for the ..Member property all I need to do is..... v is Variant = JSONToVariant(JSON) FOR EACH x OF v..Member Trace("The member " + x..Name + " has the value " + x..Value) ENDLine 1 is OK - the debugger shows 'v' as being 'an array of 5 variant'
Double clicking this shows a 5 element array with the correct values.

Line 2 however throws the error 'The variant is not an object'

Question is - what needs to be done to make the variable seen as an object?
I have tried declaring it locally in the procedure, global to the window, even global to the application - all fail with the same error.

If I declare a structure containing 'ID' and an array for this this structure then bu using
MyArray<=v fills the array with the 5 elements as expected.
So not the end of the world - it just seemed that this would be a simpler way of achieving the same result
Publicado el 21,agosto 2016 - 17:08
Looks like it is seeing it as an array of variants instead of as a variant.

This works for me:
sJSon is ANSI string = [ { "ID":"65", "ID":"67", "ID":"71", "ID":"74", "ID":"76" } ] v is Variant = JSONToVariant(sJSon) FOR EACH x OF v..Member Trace("The member " + x..Name + " has the value " + x..Value) END
This is a handy link to validate/pretty-print json:

http://codebeautify.org/jsonviewer#

Your example looks odd to me also in that I'm used to seeing unique key/value pairs, more like:
{ "ID1":"65", "ID2":"67", "ID3":"71", "ID4":"74", "ID5":"76" }
Use the above link and convert from json to xml to see how it renders them differently.

...jack
Publicado el 22,agosto 2016 - 15:17
Hi Derekt,

Variants are tricky to use. Your example is composed of an array of variants. So you must consider that array first. The following code should work with your example:

FOR i = 1 _TO_ v..Occurrence FOR EACH x OF v..Member Trace("The member " + x..Name + " has the value " + x..Value) END END
Best regards,
Alexandre Leclerc

Edit 1: Typo corrections.
Publicado el 22,agosto 2016 - 15:55
Guys
Thank you very much.
As you both no doubt noticed I let myself fall into the trap of attempting to use something without first getting a measure of understanding of how it actually works:sneg: - note to self.

Obviously I have a way to go but your examples have helped me to understand this current issue.
I did review the code used to generate the JSON string and, as you both said, was the values of a variant that was populated by and array of structures - square brackets around the string seem to be the clue here.

Going to set aside a day or two to get to grips with this as very shortly I need to commucate with a web service using only JSON.

Codebeautify.org was a great link.

Thanks again.
Publicado el 20,octubre 2016 - 10:56
Hi Alexandre,
thank you for the tip, I've tested it and it's missing [i] in the v[i]..membre statment if I'm not wrong,

this works for me

FOR i = 1 _TO_ v..Occurrence FOR EACH x OF v[i]..Member Trace("The member " + x..Name + " has the value " + x..Value) END END

RL