Sample code for 30+ languages & platforms
B4X Requires Chilkat v11.4.0+

Example: JsonObject.NewObjectOf method

Demonstrates the NewObjectOf method.

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

Dim car As String = "{" & Chr(34) & "make" & Chr(34) & ":" & Chr(34) & "Toyota" & Chr(34) & "," & Chr(34) & "model" & Chr(34) & ":" & Chr(34) & "Camry" & Chr(34) & "," & Chr(34) & "year" & Chr(34) & ":2022," & Chr(34) & "color" & Chr(34) & ":" & Chr(34) & "silver" & Chr(34) & "," & Chr(34) & "engine" & Chr(34) & ":{" & Chr(34) & "type" & Chr(34) & ":" & Chr(34) & "inline-4" & Chr(34) & "," & Chr(34) & "fuel" & Chr(34) & ":" & Chr(34) & "gasoline" & Chr(34) & "," & Chr(34) & "horsepower" & Chr(34) & ":203}," & Chr(34) & "features" & Chr(34) & ":[" & Chr(34) & "bluetooth" & Chr(34) & "," & Chr(34) & "backup camera" & Chr(34) & "," & Chr(34) & "adaptive cruise control" & Chr(34) & "," & Chr(34) & "lane assist" & Chr(34) & "]," & Chr(34) & "isElectric" & Chr(34) & ":false}"

Dim json As ChilkatJsonObject
json.Initialize

json.UpdateString("test", "abc")

success = json.NewObjectOf("car", car)
If success = False Then
    Log(json.LastErrorText)
    Return
End If


json.EmitCompact = False
Log(json.Emit)

'  Result:

'  {
'    "test": "abc",
'    "car": {
'      "make": "Toyota",
'      "model": "Camry",
'      "year": 2022,
'      "color": "silver",
'      "engine": {
'        "type": "inline-4",
'        "fuel": "gasoline",
'        "horsepower": 203
'      },
'      "features": [
'        "bluetooth",
'        "backup camera",
'        "adaptive cruise control",
'        "lane assist"
'      ],
'      "isElectric": false
'    }
'  }