JsonDeserialize
Parse a string of JSON into a Properties collection or dynamic object.
Syntax
JsonDeserialize(<string>[, <object>]) As Properties
Parameters
<string>
Type: String
The JSON string to be parsed.
<object>
Type: Object
(Optional.) An existing object to be used as the target for the deserialization. This could be a new object, for example an empty mrProperties.Properties object, or an existing object with values. Deserialization creates the structure of the target object as it reads the string.
The only valid target objects are objects returned by CreateObject() or Properties collections (mrProperties.Properties).
(return)
Type: Properties
A Properties collection or dynamic object representing the JSON string.
Example
This example shows the difference between using the TargetObject and the return Properties collection.
Example template.json file read by the example script:
{
"TemplateName": "PeopleWorking",
"Color": "Red",
"Logo": "PeopleWorkingRed.png",
"Categories": [23],
"Values": ["Hello", 42.01],
"Font": {
"Face": "Arial",
"Size": 12,
"Italics": true
}
}
Example script:
Dim Properties, JsonObject
Dim JSON
Dim FSO, TextStream
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TextStream = FSO.OpenTextFile("template.json", 1, False, -1) ' 1=ForReading, -1=OpenAsUnicode
JSON = TextStream.ReadAll()
TextStream.Close()
Set Properties = CreateObject("mrProperties.Properties")
JsonDeserialize(JSON, Properties)
Debug.Log("Accessing JSON as Properties")
Debug.Log("Template name: " + Properties["TemplateName"])
Debug.Log("Logo: " + Properties["Logo"])
Debug.Log("Font face: " + Properties["Font"].Value["Face"])
Debug.Log("Font size: " + CText(Properties["Font"].Value["Size"]))
Debug.Log("Font italics: " + CText(Properties["Font"].Value["Italics"]))
Set JsonObject = JsonDeserialize(JSON)
Debug.Log(mr.CrLf + "Accessing JSON return")
Debug.Log("Template name: " + JsonObject["TemplateName"])
Debug.Log("Logo: " + JsonObject["Logo"])
Debug.Log("Font face: " + JsonObject["Font"].Item["Face"])
Debug.Log("Font size: " + CText(JsonObject["Font"].Item["Size"]))
Debug.Log("Font italics: " + CText(JsonObject["Font"].Item["Italics"]))
See also