iOS Question Problem parsing json

Jack Cole

Well-Known Member
Licensed User
Longtime User
I'm having trouble with a map created from a parsed JSON string. See the code below.

B4X:
'executed from javascript
Sub ShowSleepLog(Record As String, LastEntryRecord As String)

#if b4i
    Dim su As StringUtils
    Record=su.DecodeUrl(Record,"UTF8")
    LastEntryRecord=su.DecodeUrl(LastEntryRecord,"UTF8")
#end if
    LogColor(Record,0xFFC67E13)
    LogColor("Last entry: "&LastEntryRecord,0xFF8300FF)
    'parse out the last entry record if there is one
    If LastEntryRecord.Length>0 Then
        Dim j As JSONParser
        j.Initialize(LastEntryRecord)  '
        Dim m As Map = j.NextObject
'        m.Remove("entered")
        Log("m.ContainsKey" & m.ContainsKey("entered"))
        Task_sleeplog.LastEntryRecordMap=m
        Task_sleeplog.LastEntryRecordMap.Remove("entered")
        Log(Task_sleeplog.LastEntryRecordMap.ContainsKey("entered"))
    End If
End Sub

Here is the value for LastEntryRecord.
B4X:
{"bedtime":"07:28","uptime":"15:28","sleeponsetmins":5,"wasomins":13,"awakeningscount":3,"qualityrating":2,"morningfeelingrating":null,"sadnessrating":0,"stressrating":0,"medsused":null,"medname":null,"comment":null,"id":"ext-record-119","tib":8,"tst":7.7,"se":96,"entered":"2019-09-10T00:00:00","updated":"2019-09-10T15:29:22","authenticatedUserId":8,"create":false,"update":false,"remove":false,"remoteid":24041}

After trying to remove the "entered" key, it is still there. If I copy the string from the log and put it directly into the code where LastEntryRecord is used to initialize the parser, it works. Any ideas?
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
After trying to remove the "entered" key, it is still there.
i guess it is because j.NextObject returns a readonly map.

Create a map manually, copy all keys from the NextObject returned Map to the new map.
Delete the key you want from this map.

B4X:
    Dim fmap As Map ' create a writeable map
    fmap.Initialize ' initialize it
    Dim j As JSONParser
    j.Initialize($"{"bedtime":"07:28","uptime":"15:28","sleeponsetmins":5,"wasomins":13,"awakeningscount":3,"qualityrating":2,"morningfeelingrating":null,"sadnessrating":0,"stressrating":0,"medsused":null,"medname":null,"comment":null,"id":"ext-record-119","tib":8,"tst":7.7,"se":96,"entered":"2019-09-10T00:00:00","updated":"2019-09-10T15:29:22","authenticatedUserId":8,"create":false,"update":false,"remove":false,"remoteid":24041}"$)
    Dim m As Map = j.NextObject
    '        m.Remove("entered")
    For Each k As Object In m.Keys ' copy all keys to the manually created map fmap
        fmap.Put(k,m.Get(k))
    Next
    Log("fmap.ContainsKey" & fmap.ContainsKey("entered")) ' entered exists
    fmap.Remove("entered") ' remove entered
    Log(fmap.ContainsKey("entered")) ' entered does not exists anymore

Ps: Tested on B4J but i expect it works the same in b4i. Don´t know as i do not use b4i
 
Upvote 0

Jack Cole

Well-Known Member
Licensed User
Longtime User
Will try that, but it worked fine for me when I used the string directly in the initialize method (like you did above). The problem comes in when the variable is used. B4A works fine for removing the key, so it doesn't seem to be read only.
 
Upvote 0
Top