Android Question Case statement & json

jchal

Active Member
Licensed User
Longtime User
Hi all
Can anybody tell me how can i fill a case statement with a jso file?
fir example my json is
Json file:
{
"myList": [
{
"Case1": "This is Case 1",
"Action1": "This is line 1",
"Bitmap": 1.bmp
},
{
"Case2": "This is Case 2",
"Action2": "This is line 2",
"Bitmap": 2.bmp
},
{
"Case3": "This is Case 3",
"Action3": "This is line 3",
"Bitmap": 3.bmp
},
{
"Case4": "This is Case 4",
"Action4": "This is line 4",
"Bitmap": 4.bmp
},
{
"Case5": "This is Case 5",
"Action5": "This is line 5",
"Bitmap": 5.bmp
},
{
"Case6": "This is Case 6",
"Action6": "This is line 6",
"Bitmap": 6.bmp
},
{
"Case7": "This is Case 7",
"Action7": "This is line 7",
"Bitmap": 7.bmp
}
]
}

And the case statemet is

B4X:
	Select ActivityListMenu.result
		Case "Case1"
			Log (Action1)
		Case "Case2"
			Log (Action2)
		Case "Case3"
			Log (Action3)
		Case "Case4"
			Log (Action4)
		Case "Case5"
			Log (Action5)
		Case "Case6"
			Log (Action6)
		Case Else
			Log("Larger than 9")
	End Select
Is it possible to create a case statement having parameters from json file?
And every time i change the json file the case statment will change without me having to go and ammend the code?
 

DonManfred

Expert
Licensed User
Longtime User
Parse the jsonfile, set variables based on it and use this variables then.
 
Upvote 0

jchal

Active Member
Licensed User
Longtime User
the point is to undestand how can i fill the case statement with the json file, how can i do it ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim jRoot As Map = parser.NextObject
Dim myList As List = jRoot.Get("myList")
For Each colmyList As Map In myList
 Dim Bitmap As String = colmyList.Get("Bitmap")
 Dim Action1 As String = colmyList.Get("Action1")
 Dim Case1 As String = colmyList.Get("Case1")
Next
 
Upvote 0

jchal

Active Member
Licensed User
Longtime User
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim jRoot As Map = parser.NextObject
Dim myList As List = jRoot.Get("myList")
For Each colmyList As Map In myList
 Dim Bitmap As String = colmyList.Get("Bitmap")
 Dim Action1 As String = colmyList.Get("Action1")
 Dim Case1 As String = colmyList.Get("Case1")
Next
after that how can i make the case statement using thoes variables?
 
Upvote 0

jchal

Active Member
Licensed User
Longtime User
it does not seen to work what I am doing wrong
B4X:
    Dim parser As JSONParser
    parser.Initialize("<text>")
    Dim jRoot As Map = parser.NextObject
    Dim myList As List = jRoot.Get("myList")
    
    Select ActivityListMenu.result
        For Each colmyList As Map In myList
        Bitmap = colmyList.Get("Bitmap")
        Action1  = colmyList.Get("Action1")
        Case1 = colmyList.Get("Case1")
    
        Case Case1
            Log(Action1)
    
    Next
            Case Else
        Log("Larger than 9")
    End Select
when i compile it says
B4A Version: 11.50
Parsing code. (0.10s)
Java Version: 11
Building folders structure. (0.04s)
Running custom action. (0.09s)
Compiling code. Error
Error compiling program.
Error description: Syntax error.
Error occurred on line: 62
For Each colmyList As Map In myList
Word: for each
I dont understant what i am doing wrong?, (if i omit the case statment it works tha means that it reads the json file corect ). it must be somthing in syndax i do wrong or is the logic wrong?
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
It is very tempting to go straight into the details and lose the target in doing so. Why do you test with the case statement on the caseX variables that also contain the value of the variable X in the CaseX, ActionX and Bitmap text? Then it is better to supplement the standard text with the index value without writing it to the JSON file.

What is your goal what you want to achieve so that we can give the most clever hint for your solution?
 
Upvote 0

jchal

Active Member
Licensed User
Longtime User
It is very tempting to go straight into the details and lose the target in doing so. Why do you test with the case statement on the caseX variables that also contain the value of the variable X in the CaseX, ActionX and Bitmap text? Then it is better to supplement the standard text with the index value without writing it to the JSON file.

What is your goal what you want to achieve so that we can give the most clever hint for your solution?
thank you for the replay, you migth be rigth i did not explain clearly what i wanted to do, i could not express my self in the rigth way, any way i just found my mistake, In what i am trying to acheive is not good with the case statement but it works fine with an if then else simple statement.

thank you all
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Here is a B4X example with comments. You can process multiple action lists with the same code and the selection code has one option.

Step 1: Create a new B4X project

Step 2: On TableConvert you have a JSON Array Editor and Generator where you can generate your JSON string layout from a spreadsheet layout

Step 3: Delete unused colums op JSON Array Editor and Generator website to prevent ":" empty fields

Step 4: The JSON string can be used to generate the code on JSON Tree Example.

Step 5: Tune and test your program code

B4J JSON parse code example:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")

End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
   
'    --- Create a @D JSON array with listname and a Case number field
'    --- for reference because JSON writes the array in random order.
'    --- Used fieldnames:
'    ---        List: Any name you want and more then one listname is possible
'    ---        Case: Array index number
'    ---        Action: Any description what to do
'    ---        Bitmap: any bitname file you want
    Dim JsonStr As String = $"
[
    [
        "List",
        "Case",
        "Action",
        "Bitmap"
    ],
    [
        "myList1",
        1,
        "This is action 1",
        "1.bmp"
    ],
    [
        "myList1",
        2,
        "This is action 2",
        "2.bmp"
    ],
    [
        "myList1",
        3,
        "This is action 3",
        "3.bmp"
    ],
    [
        "myList1",
        4,
        "This is action 4",
        "4.bmp"
    ],
    [
        "myList1",
        5,
        "This is action 5",
        "5.bmp"
    ],
    [
        "myList1",
        6,
        "This is action 6",
        "6.bmp"
    ],
    [
        "myList1",
        7,
        "This is action 7",
        "7.bmp"
    ],
    [
        "myList1",
        8,
        "This is action 8",
        "8.bmp"
    ]
]
"$

'    --- Past the JsonStr on http://basic4ppc.com:51042/json/index.html and twink the given result code to
    Dim parser  As JSONParser
    parser.Initialize(JsonStr)
    Dim jRoot As List = parser.NextArray
    Dim n As Int = 0
    For Each coljRoot As List In jRoot
        Dim X1 As Int = 0
        For Each colcoljRoot As String In coljRoot
            X1 = X1 + 1
            Select n
               
'                --- Select an action number between 1 to 8 from example
                Case 8
                    Select X1
                       
'                        --- Third field is the action to take
                        Case 3
                           
'                            --- Show the action to take
                            Log($"Action: ${colcoljRoot}"$)
                            Dim MsgStr As String = colcoljRoot
                    End Select
            End Select
           
'            --- Log all values
'            Log(colcoljRoot)
        Next
        n = n + 1
    Next
   
'    --- Show the result
    xui.MsgboxAsync(MsgStr, "Action to take")
End Sub
 
Upvote 0
Top