-
Sep 25th, 2017, 09:07 PM
#1
Thread Starter
Hyperactive Member
Json and vb6
I feel like a nuisance lately, but here goes
I am looking to poll an http site and get back a JSON answer. I did see the vbRichClient framework has some JSON function but I am not sure how to implement.
Can someone just show me a simple solution of how to process a JSON response from vb6 using vbRichClient.
The response will include mainly simple data that is used to connect to user accounts.
Thanks
WP
-
Sep 25th, 2017, 09:18 PM
#2
Re: Json and vb6
Why not consult the documentation at the support site?
-
Sep 25th, 2017, 09:32 PM
#3
Thread Starter
Hyperactive Member
Re: Json and vb6
Originally Posted by dilettante
Why not consult the documentation at the support site?
the vbRichClient Support Site? I could not find any documentation on the JSON parser.
WP
-
Sep 25th, 2017, 09:46 PM
#4
Re: Json and vb6
I'm assuming you already have the JSON string, and you just need to get it into something more friendly for use with VB6. It's a simple matter of calling New_C.JSONDecodeToCollection(YourJsonStringVariable) to get a vbRichClient5 cCollection object filled with keys and items from the JSON string.
Here's a small example that does just that and prints out a hierarchical list of keys & items to the debug window:
Code:
Option Explicit
Private Sub Command1_Click()
Dim lo_Collection As vbRichClient5.cCollection
' I found some sample JSON to use below, but normally you would have it in a String variable (or perhaps a byte array)
' There's also a JSONDecodeToCollectionUTF8 for working with UTF8 byte arrays instead of VB6 Strings.
Set lo_Collection = New_c.JSONDecodeToCollection("{" & _
" ""glossary"": {" & _
" ""title"": ""example glossary""," & _
" ""GlossDiv"": {" & _
" ""title"": ""S""," & _
" ""GlossList"": {" & _
" ""GlossEntry"": {" & _
" ""ID"": ""SGML""," & _
" ""SortAs"": ""SGML""," & _
" ""GlossTerm"": ""Standard Generalized Markup Language""," & _
" ""Acronym"": ""SGML""," & _
" ""Abbrev"": ""ISO 8879:1986""," & _
" ""GlossDef"": {" & _
" ""para"": ""A meta-markup language, used to create markup languages such as DocBook.""," & _
" ""GlossSeeAlso"": [""GML"", ""XML""]" & _
" }," & _
" ""GlossSee"": ""markup""" & _
" }" & _
" }" & _
" }" & _
" }" & _
"}")
PrintJsonCollection lo_Collection
End Sub
Private Sub PrintJsonCollection(po_Collection As vbRichClient5.cCollection)
' Loop through the collection and print keys & values.
' If we encounter another Collection then increment the Tab character count and call this sub again recursively
Static s_Tab As Long
Dim ii As Long
For ii = 0 To po_Collection.Count - 1
If TypeOf po_Collection.ItemByIndex(ii) Is vbRichClient5.cCollection Then
s_Tab = s_Tab + 1
Debug.Print String$(s_Tab, vbTab) & po_Collection.KeyByIndex(ii) & vbNewLine & String$(s_Tab, vbTab) & String$(Len(po_Collection.KeyByIndex(ii)), "-")
PrintJsonCollection po_Collection.ItemByIndex(ii)
s_Tab = s_Tab - 1
Else
Debug.Print String$(s_Tab, vbTab) & "Key: " & po_Collection.KeyByIndex(ii) & vbTab & "Value: " & po_Collection.ItemByIndex(ii)
End If
Next ii
Debug.Print
End Sub
Hope that helps!
-
Sep 25th, 2017, 09:52 PM
#5
Thread Starter
Hyperactive Member
Re: Json and vb6
Originally Posted by jpbro
Hope that helps!
Absolutely what I was looking for, Thank you so much!!
-
Sep 25th, 2017, 09:59 PM
#6
Re: Json and vb6
My pleasure! Here's another example that does a quick and dirty conversion of a JSON string to XML (NOTE: This is not production ready, just whipped it up now - it doesn't do entity encoding for example, and may break on some JSON).
Code:
Option Explicit
Private Sub Command1_Click()
Dim lo_Collection As vbRichClient5.cCollection
Set lo_Collection = New_c.JSONDecodeToCollection("{" & _
" ""glossary"": {" & _
" ""title"": ""example glossary""," & _
" ""GlossDiv"": {" & _
" ""title"": ""S""," & _
" ""GlossList"": {" & _
" ""GlossEntry"": {" & _
" ""ID"": ""SGML""," & _
" ""SortAs"": ""SGML""," & _
" ""GlossTerm"": ""Standard Generalized Markup Language""," & _
" ""Acronym"": ""SGML""," & _
" ""Abbrev"": ""ISO 8879:1986""," & _
" ""GlossDef"": {" & _
" ""para"": ""A meta-markup language, used to create markup languages such as DocBook.""," & _
" ""GlossSeeAlso"": [""GML"", ""XML""]" & _
" }," & _
" ""GlossSee"": ""markup""" & _
" }" & _
" }" & _
" }" & _
" }" & _
"}")
PrintJsonCollectionXml lo_Collection
End Sub
Private Sub PrintJsonCollectionXml(po_Collection As vbRichClient5.cCollection)
Static s_Tab As Long
Dim ii As Long
Dim l_Key As String
If s_Tab = 0 Then Debug.Print "<!DOCTYPE glossary PUBLIC ""-//OASIS//DTD DocBook V3.1//EN"">"
For ii = 0 To po_Collection.Count - 1
If TypeOf po_Collection.ItemByIndex(ii) Is vbRichClient5.cCollection Then
Debug.Print String$(s_Tab, vbTab) & "<" & po_Collection.KeyByIndex(ii) & ">"
s_Tab = s_Tab + 1
PrintJsonCollectionXml po_Collection.ItemByIndex(ii)
s_Tab = s_Tab - 1
Debug.Print String$(s_Tab, vbTab) & "</" & po_Collection.KeyByIndex(ii) & ">"
Else
l_Key = po_Collection.KeyByIndex(ii)
If Trim$(l_Key) = "" Then l_Key = "NoKey"
Debug.Print String$(s_Tab, vbTab) & "<" & l_Key & ">" & _
po_Collection.ItemByIndex(ii) & _
"</" & l_Key & ">"
End If
Next ii
End Sub
-
Sep 26th, 2017, 12:02 PM
#7
Re: Json and vb6
Thought I'd add that you can also reference the JSON tree by key, so something like this is valid for the JSON string decoded in the previous examples:
Code:
MsgBox lo_Collection("glossary")("GlossDiv")("GlossList")("GlossEntry")("ID")
or if you prefer the ! form:
Code:
MsgBox lo_Collection!glossary!GlossDiv!GlossList!GlossEntry!ID
Both will show a window with the following value: SGML
-
Sep 26th, 2017, 12:52 PM
#8
Re: Json and vb6
One issue to be aware of whenever you use the "bang" notation to access collections is that the case-sensitivity of JSON can be an issue.
A JSON document could have both a "glossary" and a "Glossary" property. But this can become an issue when the VB6 IDE autocorrects the case of one to match what it currently has in its symbol table.
That means using String values instead of using bang is more reliable when working with JSON.
-
Sep 26th, 2017, 01:06 PM
#9
Re: Json and vb6
Ahh good advice! I never actually used the "bang" notation myself, just happened to try it here since it came to mind....was wondering if it would work for chaining multiple keys with less typing, and was pleased to see that it did.
I'm relatively new to JSON - can it have 2 keys at the same "level" with the same name? That might be an issue too...(e.g. could referring to "GlossEntry" at the 4th position be a potential collision? I'll run some test anyway to see how it is handled in vbRichClient5.
-
Sep 26th, 2017, 01:27 PM
#10
Re: Json and vb6
Arrays and objects. When the items are named, they are properties of an object. So no, an object cannot have duplicate properties. JSON array elements have no names.
-
Sep 26th, 2017, 01:30 PM
#11
Re: Json and vb6
Thanks dilettante! That avoids some potential headaches...
-
Sep 26th, 2017, 01:30 PM
#12
Re: Json and vb6
This is perfectly legal though:
-
Sep 27th, 2017, 02:33 PM
#13
Thread Starter
Hyperactive Member
Re: Json and vb6
in the example above how would I get to: glossary.title
I tried collection("glossary")("title") and did not work.
well, not exactly, but on my set of data. cannot show because its confidential, but the node layers are the same.
WP
-
Sep 27th, 2017, 02:41 PM
#14
Re: Json and vb6
Can you show your code? I just tried:
Code:
MsgBox lo_Collection("glossary")("title")
And it showed "example glossary"
Note that the keys are case sensitive!
-
Sep 27th, 2017, 02:50 PM
#15
Thread Starter
Hyperactive Member
Re: Json and vb6
sent you a private message.
-
Sep 27th, 2017, 04:09 PM
#16
Re: Json and vb6
Hi axisdj. I got your JSON string, thanks. There is a top object called Stations, and then an array of stations after that. vbRichClient5 returns JSON arrays as cCollection classes (I would have guessed it would return cArrayList objects instead, but perhaps Olaf can shine some light on this design decision). It would appear that if the item has no Key value then it can be considered an array.
Using your example JSON, I was able to get each Station ID as follows:
Code:
Sub TestJson()
Dim lo_Collection As vbRichClient5.cCollection
Dim lo_Station As vbRichClient5.cCollection
Dim ii As Long
Dim l_Json As String
l_Json = New_c.FSO.ReadTextContent("C:\users\jason\documents\jsontest.txt")
Set lo_Collection = New_c.JSONDecodeToCollection(l_Json)
For Each lo_Station In lo_Collection("stations")
Debug.Print lo_Station("id")
Next lo_Station
End Sub
Hope that helps!
-
Sep 27th, 2017, 09:17 PM
#17
Re: Json and vb6
Originally Posted by dilettante
One issue to be aware of whenever you use the "bang" notation to access collections is that the case-sensitivity of JSON can be an issue.
A JSON document could have both a "glossary" and a "Glossary" property. But this can become an issue when the VB6 IDE autocorrects the case of one to match what it currently has in its symbol table.
That means using String values instead of using bang is more reliable when working with JSON.
Just wanted to follow up on the case-sensitivity of JSON and vbRichClient5 Collections decoded from JSON strings/byte arrays. My test indicate that keys are always case-sensitive regardless of whether you are using bang notation, or accessing the properties by key. I think this makes sense since:
- The source JSON object names are themselves case sensitive.
- You are likely getting JSON from a reasonably well-defined API that provides consistently cased property/object names.
In any case, I think it's the only way to do it sensibly. If keys are allowed to be case-insensitive on the VB side, then how would you handle the situation where the JSON has both a "Glossary" and "glossary" property/object name (other than only allowing access by index #)?
One of the nice things about the vbRichClient5 cCollection over the VB6 Collection is that you can optionally have case sensitive keys, and for JSON it makes sense to always be case-sensitive. If rolling your own JSON decoder you should use the Scripting library Dictionary object (with case-insensitive keys) instead of the VB Collection object, but if I'm adding references anyway I'll stick with vbRichClient5.
-
Sep 27th, 2017, 10:39 PM
#18
Re: Json and vb6
I think you completely missed my point.
-
Sep 28th, 2017, 05:38 AM
#19
Re: Json and vb6
Yeah, maybe I did Re-reading it I see you are saying that autocorrect conversion of the case of your key/property names with bang notation might cause problems. To me it looks like the bang notation is really too risky for JSON work.
TBH I never use the ! notation myself as I find it hard to read. Thought it looked useful in this case to try and reduce keystrokes for deeply nested JSON spelunking, but now I have 2 reasons to avoid it.
-
Sep 28th, 2017, 10:01 AM
#20
Re: Json and vb6
Consuming JSON is always risky.
The main thing it suffers from is that most JSON is crapped out by script copy/pasted together by web secretaries. These are by and large people who couldn't cut it as programmers and got diverted into the web ghetto by their instructors or employers.
The case-sensitivity of JavaScript probably accounts for 50% of their debugging activity after semicolon misplacement. Contrast that with C# copy/pasters (and there are lots of them out there) where they also have the same case-sensitivity to deal with but spend far less time on it as a source of bugs. Part of that is due to better coding aptitude, part due to better tools.
The other thing JSON suffers from is inconsistent use of syntax extensions not in the spec. Interoperability headaches abound. That is why The I-JSON Message Format exists now, but few seem to be adhering to it.
-
Sep 28th, 2017, 10:29 AM
#21
Re: Json and vb6
There's a philosophical discussion to be had here regarding the merits of these "standards" that really seem to have been put together rather hastily and grown organically in order to satisfy some immediate goals, vs. something well planned, specified, tested, etc...
On the one hand it can be frustrating to work with things like JSON, but on the other hand you can get stuff done with it today. Sometimes aiming for perfect means you wind up late to the party, or once you're done and out in the real world, you discover you've been solving the wrong problems and must now start over.
Anyway, I suspect that the full scope of what the "standard" is being created for is not always known in advance, only to be discovered once it starts being used widely. Sometimes things also become the standard by accident (or at least not by any design), and solutions get shoehorned into problems where they might not be a perfect fit.
It's always easy to complain once these things become de facto standards out in the wild, and all of their flaws become readily apparent, but finding solutions to thorny problems is half the fun of programming in my opinion, so I'll try to remain optimistic.
Thanks for pointing out I-JSON. I'll certainly use it for my own web API for my VB6 software. Since i-JSON is a restricted profile of JSON (but still JSON), then there is no reason not to use it.
-
Sep 28th, 2017, 10:42 AM
#22
Re: Json and vb6
The main thing I learned is that when consuming JSON you want a library with source code so you can tweak it to conform where necessary. Most of the time you have no control over the JSON encoding itself because the server dictates what extensions must be handled.
-
Sep 28th, 2017, 10:54 AM
#23
Re: Json and vb6
I'm mostly going the other way with my stuff (I'm the server handing out the JSON), so I just want to make sure I'm being a good citizen in that direction by handing out consistent JSON (so i-JSON is the way to go in my case).
Having the source is obviously critical if you need/want to make changes yourself, having a third-party library can be useful if you'd like someone else to make those changes There's some division/offloading of labour with third party libraries that can have it's advantages so long as you are able to accept not being in complete control over how and when that happens.
Like all choices, there are pros and cons that have to be weighed and dealt with accordingly as needs arise. I'm not dogmatic about it which is why I use a mix of open source, free (as in beer) closed source, and paid closed source software. I try to choose the best tool for the job based on all its merits and shortcomings (and all my merits and shortcomings - which is why I'm still using VB6!)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|