User Tools

Site Tools


vba:cast:json

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
vba:cast:json [10.06.2015 15:23:20]
yaslaw
vba:cast:json [16.01.2020 08:44:37]
yaslaw
Line 1: Line 1:
 <​const>​ <​const>​
-    version=2.0.4 +    version=2.9.1 
-    vdate=09.06.2015+    vdate=16.01.2020
     fname=lib_json.bas     fname=lib_json.bas
     ns=%NAMESPACE%     ns=%NAMESPACE%
Line 12: Line 12:
 ==Version %%version%% %%vdate%%== ==Version %%version%% %%vdate%%==
  
-{{%%fname%%|Download %%fname%% (V-%%version%%)}}+<WRAP round download 50%> 
 +><fc #​808080>//​Das Modul hat versteckte Attribute. Damit diese aktiv übernommen werden reicht es nicht aus, den Code in ein neues Modul zu kopieren. Man muss das Modul aus der Datei nach VBA importieren.//</​fc>​ 
 +>​{{popup>:​vba:​vba_importfile.png|Bild zum Import}} 
 + 
 +{{%%fname%%|Download %%fname%% (V-%%version%%)}}</​WRAP>​
  
 Für mehrere Projekte brauche ich JSON. Ich arbeitete bisher mit [[ http://​www.ediy.co.nz/​vbjson-json-parser-library-in-vb6-xidc55680.html|VB-JSON]]. Aber irgendwie störte es mich, dafür jeweils 2 Klassen und ein Modul zu kopieren. Für mehrere Projekte brauche ich JSON. Ich arbeitete bisher mit [[ http://​www.ediy.co.nz/​vbjson-json-parser-library-in-vb6-xidc55680.html|VB-JSON]]. Aber irgendwie störte es mich, dafür jeweils 2 Klassen und ein Modul zu kopieren.
Line 25: Line 29:
 ==== obj2json ==== ==== obj2json ====
 <code vb>​Public Function obj2json( _ <code vb>​Public Function obj2json( _
-        ​ByRef iObj As Variant, _ +    ​ByRef iObj As Variant, _ 
-        Optional ByVal iQuotationMarks ​As jsonQuotationMarks ​= jqmDefault _+    Optional ByVal iEncodeParams ​As jsonEncodeParams ​= jqmDefault _
 ) As String</​code>​ ) As String</​code>​
  
 === Parameterliste === === Parameterliste ===
   ***iObj **  Das Objekt (Dictionary,​ Collection, Array), welches in ein JSON-String geschrieben werden soll   ***iObj **  Das Objekt (Dictionary,​ Collection, Array), welches in ein JSON-String geschrieben werden soll
-  ***//iQuotationMarks//** Angabe, was für einAnführungszeichen verwendet werden soll+  ***//iEncodeParams//** Angabe, was für einAnführungszeichen verwendet werden soll
  
 === Return === === Return ===
Line 38: Line 42:
 ==== json2obj ==== ==== json2obj ====
 <code vb>​Public Function json2obj( _ <code vb>​Public Function json2obj( _
-        ​ByVal iString As Variant, _ +    ​ByVal iString As Variant, _ 
-        Optional ByVal iRetType ​As jsonRetType ​= jrtDefault, _ +    Optional ByVal iParams ​As jsonDecodeParams ​= jrtDefault, _ 
-        Optional ByRef oObj As Variant ​+    Optional ByRef oObj As Variant) As Variant 
-) As Variant</​code>​+</​code>​
  
 === Parameterliste === === Parameterliste ===
   ***iString **  JSON-String,​ der geparst werden soll   ***iString **  JSON-String,​ der geparst werden soll
-  ***//iRetType//**  Steuert, ob [...] als Array oder als Collection zurückgegeben wird+  ***//iParams//**  Steuert, ob [...] als Array oder als Collection zurückgegeben wird
   ***//​oObj//​** das geparste Objekt, analog zurm Returnvalue. Auf diese Art muss man den Return-Wert nicht zuerst prüfen ob es ein Array oder ein Object ist   ***//​oObj//​** das geparste Objekt, analog zurm Returnvalue. Auf diese Art muss man den Return-Wert nicht zuerst prüfen ob es ein Array oder ein Object ist
  
Line 52: Line 56:
  
 ==== Enumerator ==== ==== Enumerator ====
-=== jsonQuotationMarks ​===+=== jsonEncodeParams ​===
 Einstellung,​ ob Strings im JSON in ' oder in " gefasst werden sollen Einstellung,​ ob Strings im JSON in ' oder in " gefasst werden sollen
 +<code vb>​Public Enum jsonEncodeParams
 +    jqmDoubleQuote = 2 ^ 1          '​Umfasst die Values mit einem "
 +    jqmSingleQuote = 2 ^ 2          '​Umfasst die Values mit einem '
 +    jqmForceObject = 2 ^ 3          '​Wandelt Arrays in Objekte (Dictionaries)
 +    jqmInternalArrayPrefix = 2 ^ 4      'Der JSON-String wird mit eigenen Prefixen bei Array und Collection mitgeliefert
 +    jqmReverseSolidusAsUnicode = 2 ^ 5  'Ein \ in einem String wird nicht als \\ sondern als Unicode gaparst
 +    jqmNoErrorForWrongType = 2 ^ 6      'Wenn ein nicht parsbarer Wert kommt, den TypeName() ausgeben
 +    jqmDefault = jqmDoubleQuote
 +End Enum</​code>​
 +
 +=== jsonDecodeParams===
 +Einstellungen,​ ob [...] in ein Array oder in eine Collection geschrieben werden soll
 <code vb>​Public Enum jsonDecodeParams <code vb>​Public Enum jsonDecodeParams
     jrtCollection = 2 ^ 1   '​Als Collection     jrtCollection = 2 ^ 1   '​Als Collection
     jrtArray = 2 ^ 2        'Als Array     jrtArray = 2 ^ 2        'Als Array
-    jrtDictionary = 2 ^ 3 'Als Dictionary +    jrtDictionary = 2 ^ 3   ​'Als Dictionary 
-    ​jrtDefault ​jrtCollection +    ​jrtNotCastValue ​2 ^ 4 'cast der Values verhindern 
-    jrtEmptyList = 2 ^ 5  '​Angabe,​ ob bei einem Leeren String eine Collection/​Array/​Dictionary zurückgegeben werden soll+    jrtEmptyList = 2 ^ 5    '​Angabe,​ ob bei einem Leeren String eine Collection/​Array/​Dictionary zurückgegeben werden soll 
 +    jrtSingle2List = 2 ^ 6  'Parst ein einzelnesItem zu einer Liste 
 +    jrtDefault = jrtArray
 End Enum</​code>​ End Enum</​code>​
- 
-=== jsonRetType=== 
-Einstellungen,​ ob [...] in ein Array oder in eine Collection geschrieben werden soll 
-<code vb>​Public Enum jsonEncodeParams 
-    jqmDoubleQuote = 2 ^ 1        '​Umfasst die Values mit einem " 
-    jqmSingleQuote = 2 ^ 2        '​Umfasst die Values mit einem ' 
-    jqmForceObject = 2 ^ 3        '​Wandelt Arrays in Objekte 
-    jqmInternalArrayPrefix = 2 ^ 4  'Der JSON-String wird mit eigenen Prefixen bei Array und Collection mitgeliefert 
-    jqmDefault = jqmDoubleQuote 
-End Enum 
-</​code>​ 
  
 ===== Anwendungsbeispiele ===== ===== Anwendungsbeispiele =====
Line 223: Line 230:
 ) )
 </​code>​ </​code>​
 +
 +=== 5) Wenn der String kein JSON-String ist ===
 +Wenn der übergeben String kein JSON-String ist, gibt es verschiedene Möglichkeiten
 +<code vb>'​Standard:​ Es wird kein Objekt erstellt
 +print_r json2obj("​abc"​)
 +<​Empty> ​
 +
 +'​String in ein Array parsen
 +print_r json2obj("​abc",​ jrtSingle2List)
 +<​Variant()> ​ (
 +    [0] => <​String>​ '​abc'​
 +)
 +
 +'​String in ein Dictionary parsen
 +print_r json2obj("​abc",​ jrtSingle2List + jrtDictionary)
 +<​Dictionary> ​ (
 +    [0] => <​String>​ '​abc'​
 +)</​code>​
 +
 +=== 6) Werte Nicht Casten ===
 +Normalerweise werdend ie Werte in passende Typen umgewandelt
 +<code vb>​print_r json2obj("​[Null,​True,​12.3]"​)
 +<​Variant()> ​ (
 +    [0] => <​Null> ​
 +    [1] => <​Boolean>​ True
 +    [2] => <​Double>​ 12.3
 +)</​code>​
 +Mittels jrtNotCastValue kann dies aber auch unterbunden werden
 +<code vb>​print_r json2obj("​[Null,​True,​12.3]",​ jrtNotCastValue)
 +<​Variant()> ​ (
 +    [0] => <​String>​ '​Null'​
 +    [1] => <​String>​ '​True'​
 +    [2] => <​String>​ '​12.3'​
 +)</​code>​
 +
 +=== 7) Leere Listen erstellen ===
 +<code vb>​print_r json2obj(null)
 +<​Empty> ​
 +
 +print_r json2obj(null,​jrtEmptyList)
 +<​Variant()> ​ ()
 +
 +print_r json2obj(empty,​jrtEmptyList)
 +<​Variant()> ​ ()
 +
 +print_r json2obj("​[1]",​jrtEmptyList)
 +<​Variant()> ​ (
 +    [0] => <​Byte>​ 1
 +)</​code>​
  
  
 ===== Code ===== ===== Code =====
-<source '%%fpath%%/%%fname%%' vb>+<WRAP center round download>​ 
 +{{%%fname%%|Download ​%%fname%% ​(V-%%version%%)}} 
 +</WRAP>
vba/cast/json.txt · Last modified: 16.01.2020 08:44:37 by yaslaw