JSON..Member property

Startbeitrag von DerekT am 21.08.2016 11: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)
END
Line 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

Antworten:

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

von L Jack Wilson - am 21.08.2016 15:08
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.

von Alexandre Leclerc - am 22.08.2016 13:17
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.

von DerekT - am 22.08.2016 13:55
Zur Information:
MySnip.de hat keinen Einfluss auf die Inhalte der Beiträge. Bitte kontaktieren Sie den Administrator des Forums bei Problemen oder Löschforderungen über die Kontaktseite.
Falls die Kontaktaufnahme mit dem Administrator des Forums fehlschlägt, kontaktieren Sie uns bitte über die in unserem Impressum angegebenen Daten.