|
Post by (X) on Nov 25, 2023 15:06:49 GMT 1
I am trying to create a demo of a 100% GB32 library solution to Load, View, Edit and Save JSON data using a Treeview control.
The essential code should be packed into this library:"Lib_JSON.lg32"
I asked BING:
BING's response:
' Create a ScriptControl object Dim scriptControl As Object Set scriptControl = CreateObject("MSScriptControl.ScriptControl") scriptControl.Language = "JScript"
' Load JSON data from a file Dim fileSystem As Object Set fileSystem = CreateObject("Scripting.FileSystemObject") Dim file As Object Set file = fileSystem.OpenTextFile("path_to_your_json_file", 1) Dim jsonResponse As String jsonResponse = file.ReadAll file.Close
' Parse JSON data Dim jsonParsed As Object Set jsonParsed = scriptControl.Eval("(" & jsonResponse & ")")
' View JSON data ' Assuming the JSON data is an array of objects Dim i As Integer For i = 0 To UBound(jsonParsed) Debug.Print jsonParsed(i)("key") ' Replace "key" with the actual key in your JSON data Next i
' Edit JSON data ' Assuming the JSON data is an array of objects jsonParsed(0)("key") = "new_value" ' Replace "key" with the actual key in your JSON data
' Save JSON data to a file Dim jsonSerialized As String jsonSerialized = scriptControl.Run("JSON.stringify", jsonParsed) Set file = fileSystem.OpenTextFile("path_to_your_json_file", 2) file.Write jsonSerialized file.Close
I am looking into why BING suggested jsonParsed should be an array...
|
|
|
Post by Roger Cabo on Nov 25, 2023 17:02:58 GMT 1
You cannot iterate through an Object in gb32. Methods are not accessible for any reason.
Dim scriptControl As Object Set scriptControl = CreateObject("MSScriptControl.ScriptControl") scriptControl.Language = "JScript"
Dim jsCode As String jsCode = "function getValueByKey(json, key) {" & #13#10 & _ " return json[key] || 'Key not found';" & #13#10 & _ "}"
scriptControl.AddCode jsCode
Dim jsonResponse As String jsonResponse = "{""key1"": ""value1"", ""key2"": ""value2""}"
Dim jsonParsed As Object Set jsonParsed = scriptControl.Eval("(" & jsonResponse & ")")
Dim searchKey As String searchKey = "key1"
Dim keyValue As String keyValue = scriptControl.Run("getValueByKey", jsonParsed, searchKey)
MsgBox "'" & searchKey & " = " & keyValue
I think you should write java code like in sql.
|
|
|
Post by Roger Cabo on Nov 25, 2023 18:15:55 GMT 1
XML:
Let watch this XML example:
<a size="189" class="main-content" style="color:blue;"> Body <b woman="possibly"> Foot <c> Ears </c> </b> <z unwashed="don't know"> Foot <c> can fly </c> </z> <d> Arm <e> too long </e> </d> </a>
Dim xmlDoc As Object Set xmlDoc = CreateObject("MSXML2.DOMDocument")
Dim xmlPath As String xmlPath = "C:\Users\X\Downloads\xml.txt"
// Load from path °If Not xmlDoc.Load(xmlPath) Then ° MsgBox "Error loading xml file" °Else ° ''' °End If
// Set from string. In this case you can get the complete content of a tag and explore as well Dim xmlstr As String xmlstr = "<a size=""189"" class=""main-content"" style=""color:blue;""> Body" & #13#10 & _ " <b woman=""possibly""> Foot" & #13#10 & _ " <c> Ears" & #13#10 & _ " </c>" & #13#10 & _ " </b>" & #13#10 & _ " <z unwashed=""don't know""> Foot" & #13#10 & _ " <c> can fly" & #13#10 & _ " </c>" & #13#10 & _ " </z>" & #13#10 & _ " <d> Arm" & #13#10 & _ " <e> too long" & #13#10 & _ " </e>" & #13#10 & _ " </d>" & #13#10 & _ "</a>" Debug Debug xmlstr Debug Debug "---- Get the content of //b -----------------------"
If Not xmlDoc.LoadXML(xmlstr) Then MsgBox "Fehler beim Laden des XML-Strings!" Else End If
Dim node As Object Set node = xmlDoc.SelectSingleNode("//b") Debug.Show If Not node Is Nothing Then Debug node.Text Else Debug "Tag nicht gefunden" EndIf
Debug Debug "---- Now get the attributes of //a -----------------------"
Set node = xmlDoc.SelectSingleNode("//a")
If Not node Is Nothing Then Dim i As Integer For i = 0 To node.Attributes.Length - 1 Dim attr As Object Set attr = node.Attributes.Item(i) Debug "Attribut: " & attr.Name & ", Wert: " & attr.Value Next i Else Debug "Element <a> nicht gefunden" End If
Stop
|
|
|
Post by (X) on Nov 26, 2023 14:30:11 GMT 1
It seems access to JScript via GFA-BASIC 32 is a great asset. The following code seems to do everything I want, but, it only goes part way. ' Parse JSON data Dim jsonParsed As Object Set jsonParsed = scriptControl.Eval("(" & jsonResponse & ")") Then, if the JSON data is stored 'as an array', apparently, we need to switch to reading data into an array as if the JSON data will either be in nested format or record format when it could be in both or either, in the same data file. So, in my mind, I don't see the full picture of how to use JScript to 'more easily'* accomplish my goal.
* This is 'my' fault for not knowing JScript well enough.
My goal:
I have searched for BING and ChatGPT solutions but I only get half answers.
I was part way to getting a demo working a few days ago and I'm going to keep pushing in that direction.
If anyone has any suggestions or solutions feel free to 'chime in'.
|
|
|
Post by (X) on Nov 27, 2023 3:34:17 GMT 1
|
|
|
Post by Roger Cabo on Nov 27, 2023 18:14:57 GMT 1
I can provide some deeper info about JSON. There are some ugly pitfalls in JSON! --------------- * I think, you want to build up an open source project? * JSON can define Arrays inside Arrays! But the "Inside-Array" is not defined. * The char codec should be always UTF8, and not ASCII. * Can be as well UTF-16,UTF-32, ASCII. There is no info in JSON about the file format. You must simply guess by try by code! Example: Variant Array 2x2x2 { "myArray": [ [ ["Hello", 42], // Erste Ebene, 1. Array [3.14, true] // Erste Ebene, 2. Array ], [ [null, {"key": "value"}], // Zweite Ebene, 1. Array [[1, 2, 3], false] // SubArray, bool ] ] }
myArray[0][0][0] = "Hello" myArray[0][0][1] = 42 myArray[0][1][0] = true myArray[0][1][1] = null // Object or Collection = null myArray[1][0][0] = 3.14 myArray[1][0][1] = {"key": "value"} // Object type. myArray[1][1][0] = [1, 2, 3] (Here [1, 2, 3] the Sub Array) How ever this should be done. myArray[1][1][1] = false
a) In this case myArray[1][1][0] = is not defined. [1, 2, 3] could be subArray(0,0,0) or subArray(2).. b) Then we have the special chars as content like "こんにちは" You must simply guess by any self written code if its UTF8,UTF16,UTF32, ASCII The most JSON files are in UTF8 but not ASCII. c) A list of signs are should be pre "\" \" - Double quote \\ - Backslash \/ - Forward slash (though escaping forward slashes in JSON is not always necessary) \b - Backspace \f - Form feed \n - Line feed/New line \r - Carriage return \t - Horizontal tab \uXXXX - Unicode character (where XXXX is the hexadecimal Unicode number)
Perhaps this helps a bit to decide what to realize in the JSON (gfa32-lib)
|
|
|
Post by (X) on Nov 27, 2023 19:08:57 GMT 1
I do want to make "Lib_JSON.LG32" 'open source' so I will provide an uncompiled G32 library file asap. I am still working on a demo in straight GB32 code.
I am at the stage of generating a number based 'key' to 'point to' each JSON data key and value. I am using for reference, Crockford's 'one page' reference to JSON standard: www.json.org/json-en.html
|
|
|
Post by (X) on Nov 27, 2023 19:40:19 GMT 1
|
|
|
Post by (X) on Dec 11, 2023 18:48:19 GMT 1
The GFA JSON Parser code is still in it's infancy, but, as they say: "First get it working then optimize."
This GFA based JSON Parser is able to read a JSON data file and display it in a Treeview Control. What I thought was going to be a few days of work turned out to be bit of a marathon of trial and error. My goal is to squeeze all of this into a library: "Lib_JSON" with minimal hassle for the coder to implement and provide an intuitive Treeview based user interface to Load, View, Edit and Save any JSON file.
|
|
|
Post by (X) on Dec 12, 2023 17:29:42 GMT 1
Here are some of my guidelines / notes for this project. Ideally, practically none of this will be exposed to the coder or user. They should only have to deal with the Treeview and various DB like commands.
|
|
|
Post by Roger Cabo on Dec 12, 2023 21:36:48 GMT 1
Do you will make some example codes, including some easy JSON test files?
|
|
|
Post by (X) on Dec 12, 2023 23:08:24 GMT 1
Yes. I'm hoping this will be a great way to store and retrieve, view and edit data. I won't be posting the beginner code that keeps changing substantially from day to day. I am thinking this would make a great YouTube series for my YouTube channel www.youtube.com/@gfabasic32 to show prospective coders what GB32 is capable of.
|
|