|
Post by (X) on Nov 21, 2023 19:35:59 GMT 1
Here is a demonstration of a way to use GFA-BASIC 32 for Windows to parse a JSON* file.
1) To a String variable, 2) To a Hash Table, 3) To a Treeview control.
This program is by no means definitive. I've just been hacking away at this for the past 5 days and this is what I've come up with.
Some obvious things to add are:
Edit JSON data. Save to JSON format. Save to Hash Table format. Wrap into a GFA-BASIC 32 library: "Lib_JSON.LG32" more...
'################################################################################# ' ' DISCLAIMER: THIS CODE IS OFFERED AS IS FOR EDUCATIONAL PURPOSES. ' YOU ARE FREE TO USE, COPY AND DISTRIBUTE. ' THE USE OF THIS CODE IS AT YOUR OWN RISK. ' I ACCEPT NO LIABILITY NOR MAKE ANY CLAIM OF ' CORRECTNESS OR SUITABILITY IN PART OR IN WHOLE. ' '################################################################################# ' ' FILENAME Demo JSON Parser.G32 ' ' DESCRIPTION Can GB32 parse a JSON file to a Hash Table? Let's find out! ' ' ' AUTHOR (X) ' EMAIL xman.gb32@gmail.com ' WEBSITE https://gfabasic32.blogspot.com/ ' FORUM https://gb32.proboards.com/ ' STARTED 2023-11-16 Took a while to figure out how to read one byte at a ' time from a JSON file. As the Tazmanian Devil might ask:"Why for ' did they bury such a simple task in the cold!, cold! ground?" ' UPDATED 2023-11-21 Big day. Got most of what I wanted working up to ' displaying the content of the JSON file in a Treeview. ' Like I was saying:"I don't know what I like more; Eating glass or ' working with Treeview controls!" ' '#################################################################################
|
|
|
Post by (X) on Nov 21, 2023 19:43:54 GMT 1
Here are a few screen shots...
Demo JSON Parser.G32
A typical JSON file...
|
|
|
Post by Roger Cabo on Nov 24, 2023 14:55:27 GMT 1
Did you find out to set a different location by sending a new City to the website through GB32?
|
|
|
Post by (X) on Nov 24, 2023 15:04:03 GMT 1
Did you find out to set a different location by sending a new City to the website through GB32? They provide a city txt file that contains the 'CityID' codes. All you need to do is substitute the code you want. N.B.: All cities are not represented.
See this website for the city Id text file:
|
|
|
Post by Roger Cabo on Nov 24, 2023 16:44:32 GMT 1
Have a look at this: Open ":JSON_Test" for Input As # 1 Dim jsonResponse = Input$(LOF(# 1), # 1) Close # 1
MsgBox jsonResponse
Dim scriptControl As Object Set scriptControl = CreateObject("MSScriptControl.ScriptControl") scriptControl.Language = "JScript"
Dim jsonParsed As Object Set jsonParsed = scriptControl.Eval("(" & jsonResponse & ")")
Dim employee1Name As String Dim employee2Name As String
' Zugriff auf Eigenschaften des durch Eval zurückgegebenen JScript-Objekts employee1Name = jsonParsed.Employee1.name employee2Name = jsonParsed.Employee2.name
MsgBox "Employee1 Name: " & employee1Name & #13#10 & "Employee2 Name: " & employee2Name
But for real.. that's a bit strange but if you don't know the content because all the names are hard-coded. I will investigate some more into this.. Attachments:JSON_Reading.G32 (1.27 KB)
|
|
|
Post by Roger Cabo on Nov 24, 2023 17:26:07 GMT 1
JSON_Reading _02.G32 (1.62 KB) JSON_Reading _02.html (3.58 KB) This was a bit lucky!! Here is another example using Java Script In Gb32 Open ":JSON_Test" for Input As # 1 Dim jsonResponse = Input$(LOF(# 1), # 1) Close # 1
MsgBox jsonResponse
Dim scriptControl As Object Set scriptControl = CreateObject("MSScriptControl.ScriptControl") scriptControl.Language = "JScript"
Dim jsonParsed As Object Set jsonParsed = scriptControl.Eval("(" & jsonResponse & ")")
scriptControl.AddCode "function getKeyValuePairs(obj) { " & _ "var result = []; " & _ "for (var key in obj) { " & _ "if (typeof obj[key] == 'object') { " & _ "result.push(key + ':' + getKeyValuePairs(obj[key])); " & _ "} else { " & _ "result.push(key + ':' + obj[key]); " & _ "}} " & _ "return result.join('#'); }" // #<--- use any delimiter you like
Dim keyValuePairsString As String keyValuePairsString = scriptControl.Run("getKeyValuePairs", jsonParsed)
Dim pairs As Hash String Split pairs[] = keyValuePairsString, "#" //<--- use any delimiter you like Dim s$ For Each s In pairs[] Debug Each`s Next
Debug.Top Debug.Show
Stop
MSScriptControl.ScriptControl is a control to use java script inside of an object. This is more than ROFL! Never thought about this.. I will investigate some more time. Perhaps there any other crazy stuff possible.. eg using .Net in GB32 that would blown me away! Can you imagine what possible then?
|
|
|
Post by (X) on Nov 24, 2023 18:45:03 GMT 1
This is a fantastic! Do you think the same can be achieved for XML files? (I am thinking: "Yes".)
|
|
|
Post by Roger Cabo on Nov 24, 2023 22:20:49 GMT 1
An XML structure is nothing else than an if - endif structure. Starting with "<a>" ending with "</a>" Each time you open a new <anything> element, you must remember this one until it's closed with </anything> This is hardcore Here is the first attempt, but have some missing entries.. I must have a look.. because I don't know exactly what the result is. <?xml version="1.0" encoding="UTF-8"?> <university> <department name="Informatik"> <professor id="p101"> <name>Dr. Max Mustermann</name> <courses> <course code="CS101">Einführung in die Informatik</course> <course code="CS102">Algorithmen und Datenstrukturen</course> </courses> <publications> <publication year="2019" type="Journal">Artikel über KI</publication> <publication year="2020" type="Konferenz">Vortrag über Datenanalyse</publication> </publications> </professor> <student id="s201"> <name>Lisa Müller</name> <enrolledIn code="CS101"/> <enrolledIn code="CS102"/> </student> </department> <department name="Physik"> <!-- Struktur ähnlich wie Informatik, aber mit anderen Professoren, Kursen usw. --> </department> </university>
---------------------------------------------------
Element: university Element: department - Attribut: name; Wert: Informatik Element: professor - Attribut: id; Wert: p101 Element: name Element: courses Element: course - Attribut: code; Wert: CS101 Element: course - Attribut: code; Wert: CS102 Element: publications Element: publication - Attribut: year; Wert: 2019 - Attribut: type; Wert: Journal Element: publication - Attribut: year; Wert: 2020 - Attribut: type; Wert: Konferenz Element: student - Attribut: id; Wert: s201 Element: name Element: enrolledIn - Attribut: code; Wert: CS101 Element: enrolledIn - Attribut: code; Wert: CS102 Element: department - Attribut: name; Wert: Physik
XML_Reading.G32 (2.29 KB) must have a look tomorrow
|
|
|
Post by (X) on Nov 24, 2023 22:45:54 GMT 1
These are looking very promissing for incorporation into a GFA Library! I'd like to be able to Load, View, Edit and Save in JSON and XML formats with simple $Lib "Lib_JSON" and $Lib "Lib_XML" statements.
|
|
|
Post by Roger Cabo on Nov 24, 2023 22:53:11 GMT 1
another version seems to work.. XML_Reading_02.G32 (2.63 KB) But the question is.. but how you will show the developer the result of the data structure.. This must be be discussed.. This is a rough result only..
|
|
|
Post by Roger Cabo on Nov 24, 2023 22:58:02 GMT 1
Also found a solution with Website-HTML-Object to log into a web site.. and get the result page of the login was successful
|
|