### Parse JSON File Ordered

To read the content of a JSON File use an array ([{},{}]) or a key/value object ({}).
An Array is parsed using JSONParser.NextArray which returns a List of Objects.
An Object is parsed using JSONParser.NextObject which returns an Object.
JSON Array File Structure
[
{"Key1": "Value1"},
{"Key2": "Value2"},
{"Key3": "Value3"},
{"Key4": "Value4"}
]
JSON Array File parse using NextArray
' Define the parser
Dim mJSONParser As JSONParser
' Init the parser with the content of the file to parse
mJSONParser.Initialize(File.ReadString(File.DirApp, "myfile.json"))
' Get the array of json entries and assign to a list of maps
Dim root As List = mJSONParser.NextArray
' Go over the list of maps and show the key / value pair
For i = 0 To root.Size - 1
Dim m As Map = root.Get(i)
Log(i & ":" & m.GetKeyAt(0) & " = " & m.GetValueAt(0))
Next
Output from previous example
0:Key1 = Value1
1:Key2 = Value2
2:Key3 = Value3
3:Key4 = Value4
Hint:
To handle exceptions, f.e. if the content can not be parsed, use Try ... Catch ... End Try.
Like,
...
' Init the parser with the content of the file to parse
Try
mJSONParser.Initialize(File.ReadString(File.DirApp, "myfile2.json"))
' Get the array of json entries and assign to a list of maps
Dim root As List = mJSONParser.NextArray
' Go over the list of maps and show the key/ value pair
For i = 0 To root.Size - 1
Dim m As Map = root.Get(i)
Log(i & ":" & m.GetKeyAt(0) & " = " & m.GetValueAt(0))
Next
Catch
Log("Error: " & LastException.Message)
End Try
...

Changed: 2018-05-05 16:38, Category: JSON, File: T6C210M1535.htm, ID: 1535