B4J Question [BANano] [SOLVED] How to parse 'different' JSON string back to List/Map without modification?

Mashiane

Expert
Licensed User
Longtime User
Ola

I'm trying to copy and use code from javascript (which is somehow 'different'). Instead of having to modify such strings before I can use them, I'm wondering if its possible to get them parsed to list and or map variables.

B4X:
Dim sCode As String = $"[{action: 'move_to_inbox',    title: 'Inbox'},{action: 'send', title: 'Sent'},{action: 'delete', title: 'Trash'},{action: 'report', title: 'Spam'},{divider: True }, {header: 'Labels'},{action: 'label', title: 'Family'},{action: 'label', title: 'Friends'},{action: 'label', title: 'Work'}]"$

1. The keys are not inside quotes i.e action
2. There is a single quote on the titles

In normal circumstances most things should be in double quotes.

Thanks.
 

Daestrum

Expert
Licensed User
Longtime User
Uses json library

B4X:
    Dim sCode As String = $"[{action: 'move_to_inbox',    title: 'Inbox'},{action: 'send', title: 'Sent'},{action: 'delete', title: 'Trash'},{action: 'report', title: 'Spam'},{divider: True }, {header: 'Labels'},{action: 'label', title: 'Family'},{action: 'label', title: 'Friends'},{action: 'label', title: 'Work'}]"$
    Dim j,k As JSONParser
   
    j.Initialize(sCode)
   
    Dim iCount As Int = 0
   
    Dim a As List = j.NextArray
    Log(a)
    For Each item In a
        Log("------" & CRLF & "item " & iCount & CRLF &"------")
        k.Initialize(item)
        Dim m As Map = k.NextObject
        For Each mitem In m.Keys
            Log(mitem & " = " & m.Get(mitem))
        Next
        iCount = iCount + 1       
    Next

Produces
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
B4X:
Dim Parser As JSONParser
parser.Initialize("[{action: 'move_to_inbox',    title: 'Inbox'},{action: 'send', title: 'Sent'},{action: 'delete', title: 'Trash'},{action: 'report', title: 'Spam'},{divider: True }, {header: 'Labels'},{action: 'label', title: 'Family'},{action: 'label', title: 'Friends'},{action: 'label', title: 'Work'}]")

Dim Root As List = Parser.NextArray
For Each ColRoot As Map In Root
     Dim Action As String = ColRoot.Get("action")
     Dim Title As String = ColRoot.Get("title")
Next
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…