B4X
B4X
JSON Iterate Members
See more JSON Examples
Demonstrates how to loop over the immediate members of a JSON object.Chilkat B4X Downloads
Dim success As Boolean = False
Dim json As ChilkatJsonObject
json.Initialize
Dim jsonStr As String = "{ " & Chr(34) & "id" & Chr(34) & ": 1, " & Chr(34) & "name" & Chr(34) & ": " & Chr(34) & "A green door" & Chr(34) & ", " & Chr(34) & "tags" & Chr(34) & ": [" & Chr(34) & "home" & Chr(34) & ", " & Chr(34) & "green" & Chr(34) & "], " & Chr(34) & "price" & Chr(34) & ": 125 }"
success = json.Load(jsonStr)
If success <> True Then
Log(json.LastErrorText)
Return
End If
Dim numMembers As Int = json.Size
Dim i As Int
For i = 0 To numMembers - 1
Dim name As String = json.NameAt(i)
Dim value As String = json.StringAt(i)
Log(name & ": " & value)
Dim iValue As Int = json.IntAt(i)
Log(name & " as integer: " & iValue)
Next
' Note: The StringAt method returns the value as a string regardless of the type.
' If the value is a JSON array (such as for ["home", "green"]), then the JSON encoding
' of the entire array is returned.
' The IntAt method returns the value as an integer. If the value does not convert to
' an integer, then 0 is returned.