### About

To parse a JSON file use the JSON parser from the B4J JSON Library.
JSON stands for JavaScript Object Notification.
Information
What is JSON
http://json.org
Tutorial (from Basic4Android)
http://www.b4x.com/android/forum/threads/android-json-tutorial.6923/#content
Online Parser to generate B4A/B4J Code
http://www.b4x.com:51042/json/index.html
B4A BAL Convertor
http://www.b4x.com/android/forum/threads/balconverter-convert-the-layouts-files-to-json-and-vice-versa.41623/#content
Other Articles
http://www.vogella.com/tutorials/JSON/article.html
Parse Array and Object
To parse 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.
Example Parse JSON ARRAY
[
{"Key1a": "Value1a", "Key1b": "Value1b"},
{"Key2a": "Value2a", "Key2b": "Value2b"},
{"Key3a": "Value3a", "Key3b": "Value3b"}
]
`Sub parseJSONArray
' Define the parser
Dim mJSONParser As JSONParser
' Init the parser with the content of the file to parse
Try
mJSONParser.Initialize(File.ReadString(File.DirApp, "arraycontent.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
Log("Array: " & i)
Dim m As Map = root.Get(i)
For j = 0 To m.Size - 1
Log("Key / Value Pair: " & j & ":" & m.GetKeyAt(j) & " = " & m.GetValueAt(j))
Next
Next
Catch
Log("Error: " & LastException.Message)
End Try
End Sub`
Example Parse JSON OBJECT
{
"Key1a": "Value1a", "Key1b": "Value1b"
}
`Sub parseJSONObject
' Define the parser
Dim mJSONParser As JSONParser
' Init the parser with the content of the file to parse
Try
mJSONParser.Initialize(File.ReadString(File.DirApp, "objectcontent.json"))
' Get the object and assign to a map
Dim m As Map = mJSONParser.NextObject
' Go over the map and show the key / value pair
For i = 0 To m.Size - 1
Log("Key / Value Pair: " & i & ":" & m.GetKeyAt(i) & " = " & m.GetValueAt(i))
Next
Catch
Log("Error: " & LastException.Message)
End Try
End Sub`
Call previous Subs
`Sub buttonTest_Action
Log("*** JSON Parser Array")
parseJSONArray
'
Log("*** JSON Parser Object")
parseJSONObject
End Sub`
Output
*** JSON Parser Array
Array: 0
Key / Value Pair: 0:Key1b = Value1b
Key / Value Pair: 1:Key1a = Value1a
Array: 1
Key / Value Pair: 0:Key2a = Value2a
Key / Value Pair: 1:Key2b = Value2b
Array: 2
Key / Value Pair: 0:Key3a = Value3a
Key / Value Pair: 1:Key3b = Value3b
*** JSON Parser Object
Key / Value Pair: 0:Key1b = Value1b
Key / Value Pair: 1:Key1a = Value1a
Hint:
To handle exceptions, f.e. if the content can not be parsed, use Try ... Catch ... End Try.
Another Example JSON File Parse OBJECT
JSON File:
{
host:192.168.0.23,
port:4280,
uid:nWr,
switches:5,
switch1:{name:"switch1", label:"Basement", hc:1, rc:1},
switch2:{name:"switch2", label:"Guest Bedroom", hc:1, rc:2},
switch3:{name:"switch3", label:"Garage", hc:1, rc:3},
switch4:{name:"switch4", label:"Server Room", hc:1, rc:4},
switch5:{name:"switch5", label:"Pantry", hc:1, rc:5}
}
Parse Code:
...
Private gstrSettingsFile As String = "rswitchit.json"
Private gmapSettings As Map
Private glstSwitches As List
...
Dim parser As JSONParser
Dim tr As TextReader
Dim str As String
' Read the content of the JSON file in a string
Try
tr.Initialize(File.OpenInput(File.DirApp, gstrSettingsFile))
str = tr.ReadAll
tr.Close
Catch
Log("Error reading configuration file:" & LastException.Message)
Return
End Try
' Ini the json parser and read next object
parser.Initialize(str)
gmapSettings = parser.NextObject
' Clear the switches list
glstSwitches.Clear
For i = 0 To gmapSettings.get("switches") - 1
glstSwitches.Add(gmapSettings.get("switch" & (i + 1) ))
Next
' Assign the swiches labeltext to the switches (see index.html)
Dim switches As Int = gmapSettings.Get("switches")
For i = 0 To switches - 1
Dim sw As Map = glstSwitches.Get(i)
If i = 0 Then lblswitch1.SetText(WebUtils.EscapeHtml(sw.Get("label")))
If i = 1 Then lblswitch2.SetText(WebUtils.EscapeHtml(sw.Get("label")))
If i = 2 Then lblswitch3.SetText(WebUtils.EscapeHtml(sw.Get("label")))
If i = 3 Then lblswitch4.SetText(WebUtils.EscapeHtml(sw.Get("label")))
If i = 4 Then lblswitch5.SetText(WebUtils.EscapeHtml(sw.Get("label")))
Next
...

Changed: 2018-06-07 09:14, Category: JSON, File: T6C210M1533.htm, ID: 1533