On the website http://de.wikipedia.org/wiki/JavaScript_Object_Notation, the data format JSON is described thus: The JavaScript Object Notation, or JSON [ˈdʒeɪsən], is a compact data format in an easy-to-read text form for the purpose of exchanging data between applications. Any valid JSON document shall be a valid JavaScript and be able to be interpreted via eval(). […] Apart from that, however, JSON is independent of the programming language.
The class JSON (gb.web → gb.util.web) allows you to encode and decode in JSON format.
The following table describes the mapping between JSON elements and Gambas data types:
JSON syntax | JSON element | Gambas data type |
---|---|---|
{ “string”: value , … } | object | collection |
[ value, … ] | Array | Variant[ ] |
“abc” | String | UTF-8-String |
-123.45E+6 | Number | Integer, Long or Float |
true or false | Truth value (Boolean) | Boolean |
zero | Zero | ZERO or JSON.Zero |
Table 24.6.9.0.1 : Mapping table
Note that JSON syntax requires you to lowercase the values true, false and null in the JSON text.
The JSON class has only one property and two methods:
The class JSONCollection (gb.web → gb.util.web) represents a JSON object.
The JSONCollection class has two properties:
Property | DataType | Description |
---|---|---|
Count | Integer | Returns the number of elements stored in the JSON collection. |
Length | Integer | Synonym for the property Count. |
Key | String | Returns the key of the last read or enumerated element in a JSON collection. |
Table 24.6.9.0.2 : Properties of the class JSONCollection
The JSONCollection class has the following five methods:
method | description |
---|---|
Add ( Value As Variant, Key As String ) | Inserts an element as a value-key pair into a JSON collection. |
Clear ( ) | Deletes the content of a JSON collection. |
Copy ( ) | Returns a 1:1 copy of a JSON collection as a standalone object. |
Exist ( Key As String ) As Boolean | Returns True if a value exists in a JSON collection for the key passed as parameter 'Key'. |
Remove ( Key As String ) | Deletes the element with the key passed as parameter 'Key' in a JSON collection. |
Table 24.6.9.0.3 : Methods of the class JSONCollection
In this example, data is converted to JSON format using the JSON.Decode(….) method:
[1] Dim fRatio As Float, sJSONText As String, vArray As Variant[] [2] Dim cCollection As Collection, cJSONCollection As JSONCollection [3] [4] cJSONCollection = New JSONCollection [5] cCollection = New Collection [6] [7] fRatio = 0.1 [8] cJSONCollection["Seite \"a\" "] = 688.7 * fRatio [9] cCollection[String.Chr(946)] = 43 [10] cCollection[String.Chr(947)] = 2.039E1 [11] cJSONCollection["Winkel"] = cCollection ' Alternative: cData.Add(cCollection, "Winkel") [12] cJSONCollection["Umfang Dreieck " & String.Chr(916) & " ABC"] = "?" [13] cJSONCollection["Widerstand in " & String.Chr(937)] = 2550 [14] vArray = New Variant[] [15] vArray = ["gambas-buch.de", "gambas.sourceforge" & ".net"] [16] cJSONCollection["URLs"] = vArray [17] cJSONCollection["EMail"] = "wer@ist.da" [18] cJSONCollection["Datum"] = Format(Now(), "dd. mmmm yyyy") [19] cJSONCollection["Diplom"] = False [20] cJSONCollection["Master"] = JSON.Null [21] [22] sJSONText = JSON.Encode(cJSONCollection) [23] [24] Print sJSONText
Comment:
This is how the JSON text presents itself in the console of the Gambas IDE:
{"Seite \"a\" ": 68.87, "Winkel": {"β": 43, "γ": 20.39}, "Umfang Dreieck Δ ABC": "?", "Widerstand in Ω": 2550, "URLs": ["gambas-buch.de", "gambas.sourceforge.net"], "EMail": "wer@ist.da", "Datum": "17. April 2016", "Diplom": false, "Master": null}
If the JSON text is output formatted → Chapter 24.6.9.1 Project formatter for JSON text, it is easier to read:
{ "Seite "a" ": 68,87, "Winkel": { "β": 43, "γ": 20,39 }, "Umfang Dreieck Δ ABC": "?", "Widerstand in Ω": 2550, "URLs": [ "gambas-buch.de", "gambas.sourceforge.net" ], "EMail": "wer@ist.da", "Datum": "17. April 2016", "Diplom": false, "Master": null }
If one analyses the value data type of the elements in the JSON collection, then the following overview shows up:
Anzahl der Elemente in der JSON-Collection: 9 ---------------------------------------------------------------------------------------------------- Schlüssel 1 : "Seite "a" " ---> Wert-Typ: Float Schlüssel 2 : "Winkel" ---> Wert-Typ: JSONCollection Schlüssel 3 : "Umfang Dreieck Δ ABC" ---> Wert-Typ: String Schlüssel 4 : "Widerstand in Ω" ---> Wert-Typ: Integer Schlüssel 5 : "URL" ---> Wert-Typ: Variant[] Schlüssel 6 : "EMail" ---> Wert-Typ: String Schlüssel 7 : "Datum" ---> Wert-Typ: String Schlüssel 8 : "Diplom" ---> Wert-Typ: Boolean Schlüssel 9 : "Master" ---> Wert-Typ: NULL oder JSON-Null
Note: In revision 7744 of Gambas3, a bug has been fixed in the JSONCollection class that did not correctly take the JSON.Null property.