B4J Question java.lang.RuntimeException: parsing map value to boolean

i am get an error when i try to cast a value in a map to a boolean variable .
The data is from a JSON string which i parsed into a map and it contains a boolean field.

Here is the code snippet which generates the error:

B4X:
Dim parser As JSONParser
    Dim JSONCategoriesMap As Map
    parser.Initialize(JSONstoredAllCategories)
    JSONCategoriesMap=parser.NextObject
    Dim categorieslist As List =JSONCategoriesMap.get("categories")
   
    For Each field As Map In categorieslist
       
        Dim categoryactive As Boolean =field.Get("is_active").As(Boolean)


The error from the logs is this one:

java.lang.RuntimeException: Cannot parse: 1 as boolean

I am new to the B4X language and any help would be appreciated.
 

Daestrum

Expert
Licensed User
Longtime User
You could try something like
B4X:
Dim categoryactive As Boolean = IIF(field.Get("is_active")=1,true,false)

A Java Boolean has no numeric value, it's either true or false (unlike other languages which can accept true/false as a numeric value)
 
Last edited:
Upvote 0
You could try something like
B4X:
Dim categoryactive As Boolean = IIF(field.Get("is_active")=1,true,false)

A Java Boolean has no numeric value, it's either true or false (unlike other languages which can accept true/false as a numeric value)
Thanks seems to be the same with the way i had solved it .

i had used this :

B4X:
If field.Get("is_active")=1 Then

but i wasn't sure if it was the best way and also i wasn't aware that a java boolean is different from other languages which have 1 or 0 for boolean variables thanks for the information.
 
Upvote 0
Top