Having a slight problem with JSON parsing, I am sure I am making a mistake somewhere but I am entirely not sure what.
Here is my JSON Payload:
{"GameOptions":{"GameOptionsClockPreset1":"","GameOptionsClockPreset2":"","GameOptionsClockPreset3":"","GameOptionsHornEnable":true,"GameOptionsScreen1":false,"GameOptionsScreen2":true,"GameOptionsSport":2,"GameOptionsLightingEnable":true,"GameOptionsRetainLighting":true}}
Which gets parsed down to this:
{GameOptionsClockPreset1=, GameOptionsClockPreset2=, GameOptionsClockPreset3=, GameOptionsHornEnable=true, GameOptionsScreen1=false, GameOptionsScreen2=true, GameOptionsSport=2, GameOptionsLightingEnable=true, GameOptionsRetainLighting=true}
From this code snippet:
Notice, sometimes there are empty strings when things arent set by the sender.
The specific code to parse it back into a Map and custom object is this:
The trouble is, I am getting this error:
(JSONException) org.json.JSONException: Expected literal value at character 25 of {GameOptionsClockPreset1=, GameOptionsClockPreset2=, GameOptionsClockPreset3=, GameOptionsHornEnable=true, GameOptionsScreen1=true, GameOptionsScreen2=false, GameOptionsSport=2, GameOptionsLightingEnable=true, GameOptionsRetainLighting=true}
I assume its due to the empty strings which shouldnt be an issue as they are supposed to be blank sometimes, almost as if the quotes are being stripped out of the payload.
Not sure where to go from here. Thanks!
Here is my JSON Payload:
{"GameOptions":{"GameOptionsClockPreset1":"","GameOptionsClockPreset2":"","GameOptionsClockPreset3":"","GameOptionsHornEnable":true,"GameOptionsScreen1":false,"GameOptionsScreen2":true,"GameOptionsSport":2,"GameOptionsLightingEnable":true,"GameOptionsRetainLighting":true}}
Which gets parsed down to this:
{GameOptionsClockPreset1=, GameOptionsClockPreset2=, GameOptionsClockPreset3=, GameOptionsHornEnable=true, GameOptionsScreen1=false, GameOptionsScreen2=true, GameOptionsSport=2, GameOptionsLightingEnable=true, GameOptionsRetainLighting=true}
From this code snippet:
B4X:
Type ScoreCommand (Command As String, Data As Object) 'Packet sent between main device and stat remote device
'The JSON packet should contain the ScoreCommand Object in text format, which is the Command itself, and the Value.
JSON.Initialize(BytesToString(Buffer, 0, Buffer.Length, "UTF-8"))
Dim jRoot As Map = JSON.NextObject
If jRoot.Size = 1 Then 'Maximum and minimum of 1 item in the map. The top object is always a ScoreCommand type.
' Dim ScoreCommandMap As Map = jRoot.Get(0) 'Only the first element of the array is necessary, and should contain our scorecommand.
Dim DataPacket As ScoreCommand
DataPacket.Initialize
DataPacket.Command = jRoot.GetKeyAt(0)
DataPacket.Data = jRoot.GetValueAt(0)
Else 'Return on failure
Return
End If
Notice, sometimes there are empty strings when things arent set by the sender.
The specific code to parse it back into a Map and custom object is this:
B4X:
Type GameOptionsData(HornEnable As Boolean, Screen1 As Boolean, Screen2 As Boolean, ClockPreset1 As String, ClockPreset2 As String, ClockPreset3 As String, Sport As Int, LightingEnable As Boolean, RetainLighting As Boolean)
Public GameOptions As GameOptionsData
Try
Dim JSON As JSONParser
JSON.Initialize(DataPacket.Data)
Dim ScoreData As Map = JSON.NextObject
Common.MapToGameOptions(ScoreData, GameOptions)
Catch
Log(LastException)
End Try
'Convert Map to Game Options
Sub MapToGameOptions(V2Data As Map, GameOptions As GameOptionsData)
If V2Data.ContainsKey("GameOptionsClockPreset1") Then GameOptions.ClockPreset1 = V2Data.Get("GameOptionsClockPreset1")
If V2Data.ContainsKey("GameOptionsClockPreset2") Then GameOptions.ClockPreset2 = V2Data.Get("GameOptionsClockPreset2")
If V2Data.ContainsKey("GameOptionsClockPreset3") Then GameOptions.ClockPreset3 = V2Data.Get("GameOptionsClockPreset3")
If V2Data.ContainsKey("GameOptionsHornEnable") Then GameOptions.HornEnable = V2Data.Get("GameOptionsHornEnable")
If V2Data.ContainsKey("GameOptionsScreen1") Then GameOptions.Screen1 = V2Data.Get("GameOptionsScreen1")
If V2Data.ContainsKey("GameOptionsScreen2") Then GameOptions.Screen2 = V2Data.Get("GameOptionsScreen2")
If V2Data.ContainsKey("GameOptionsSport") Then GameOptions.Sport = V2Data.Get("GameOptionsSport")
If V2Data.ContainsKey("GameOptionsLightingEnable") Then GameOptions.LightingEnable = V2Data.Get("GameOptionsLightingEnable")
If V2Data.ContainsKey("GameOptionsRetainLighting") Then GameOptions.RetainLighting = V2Data.Get("GameOptionsRetainLighting")
End Sub
The trouble is, I am getting this error:
(JSONException) org.json.JSONException: Expected literal value at character 25 of {GameOptionsClockPreset1=, GameOptionsClockPreset2=, GameOptionsClockPreset3=, GameOptionsHornEnable=true, GameOptionsScreen1=true, GameOptionsScreen2=false, GameOptionsSport=2, GameOptionsLightingEnable=true, GameOptionsRetainLighting=true}
I assume its due to the empty strings which shouldnt be an issue as they are supposed to be blank sometimes, almost as if the quotes are being stripped out of the payload.
Not sure where to go from here. Thanks!
Last edited: